Received: by 2002:ab2:3350:0:b0:1f4:6588:b3a7 with SMTP id o16csp1188545lqe; Mon, 8 Apr 2024 01:00:29 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUEPy3BV8jOJee2NfYmxgX2TfM7oWADK6dHvWBnvtS4BIs1YbAWflfFXoPGnGYroucRt11U+W3whhbv8dKSuE+sYWZGkmM4OlY9k05uTg== X-Google-Smtp-Source: AGHT+IGCG0QwSA5p//sGnqcp3OgMkeOoCY8BlRMKgASE5fEXeBa95WOJQExCgtqCxjjnByLG9FGs X-Received: by 2002:a05:6214:250b:b0:69b:1be3:e76f with SMTP id gf11-20020a056214250b00b0069b1be3e76fmr2486601qvb.44.1712563229540; Mon, 08 Apr 2024 01:00:29 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712563229; cv=pass; d=google.com; s=arc-20160816; b=rjk+2a+yO7KDuwOHeNjhyLnDLGvi7VUstZoslGtSIU4VHygZ9MeIW8gLl0ilQIFvXr Tg8PGLYPOSGaMF9oyWIeAxT6yufPoGtJAxYLoS5PHMW6U5favYFVdnSqusJuHG3xw8Ky tLVNY7iyJ6TYxCuJPIt9SSjYGvWwx7HlINx4Tld49AwTWrmrJpcjz0y6cck2aEABZ3R/ Ns6MjlfSqph7WiLDHPQCgdJ292X5GQtx3sKoL1UkXfMd+AK4syUIFgKiUZzSf1+JOda7 6yhsZKM6LGr6Og5WNc+P3UbE0YoZFi6dqF0eyDtconKDkLygg6tuzrM5x+Dae6SkG6l8 O3KA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=PbG/Cv7DGpt7tAVi1gqaOi//Mh/4FAflEt7ny+NUGq4=; fh=qx731vA9jE8TwE8/P0HOqS2dTHbr4xlFUgxYMFBwu9A=; b=gliRHb+cOF1iSLtkLSjNIpug9RyLKTXZ8zACmIV1pzf0kBSujC479iIBUxMAim1xU0 Kn698qWjd8HShG/FOHPxZnQqdveck9I/iQRoEXOuhI2R3x4oC7tKZPfA8jQOCOUOYuRZ iCZO1JQxpbSFUOKWvjkSL9N7LrRxnqc7Nk2cM+kHYvuAyZWjirTMRAeXVdFD5u/TdEom k9T87sV64vH4J7qxh/4DqIxDFM+KgsgJfB6lbz/kyxYuV2Gfv27x/GEr03HXqZrENneg thxMEtkLaGyLd4GO2gv5ddNgTnOBCf6sHxmo3zfDQiCCV8pbfJUJraSQDZlyfAnYU7A3 HiOw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=a2LpuEzF; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-134982-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-134982-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id e9-20020a0cf349000000b0069b2068681csi622560qvm.495.2024.04.08.01.00.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Apr 2024 01:00:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-134982-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=a2LpuEzF; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-134982-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-134982-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 354321C21710 for ; Mon, 8 Apr 2024 08:00:29 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 43A0C2D057; Mon, 8 Apr 2024 07:59:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="a2LpuEzF" Received: from mail-vk1-f175.google.com (mail-vk1-f175.google.com [209.85.221.175]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ABE392C68C for ; Mon, 8 Apr 2024 07:59:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.175 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712563191; cv=none; b=k3h/Vl8iQsNTjZtOnvIfnUvQ4iiLKDOLMq7gyCpggl5Q2Sw1JgOdmMyWIh0VrvS3JSeoIZis4UYIhZ/qTZcPOzWndPvGAbHKFcJM8wSscOC1gGZd1e1n/bgZozuCA6+mx8udhi5fV56q3QxFRA8nn9qLTLxHU1ltlHxjWCVGXB8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712563191; c=relaxed/simple; bh=gfPu8CWZl3E7Z4ROJvqIJZLka/gChnqmeOgsGF6qUas=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=MKQxeLYtUg/BSfTOlboxGAIu6RAjLZpw1p74BUORtIcfo2y+JtaWGmTvz9Ldd6zdbNlfpnaifQHa05AFmCDFoxs2LOCvytDs/JDQErPSCcz9WOfaAbNvdoNfwLbN1IIVmkrnYQjZBk52nJ1FdpE1JyhN0u8gRonReSj5ENkYDjo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=a2LpuEzF; arc=none smtp.client-ip=209.85.221.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Received: by mail-vk1-f175.google.com with SMTP id 71dfb90a1353d-4dac4791267so516129e0c.1 for ; Mon, 08 Apr 2024 00:59:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1712563189; x=1713167989; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=PbG/Cv7DGpt7tAVi1gqaOi//Mh/4FAflEt7ny+NUGq4=; b=a2LpuEzFh+1zrDKHS9Qgvdy+fcXpBktWbM/dc2JyfeklFRcuNlthqgqXYvu2PTBmMv ayP5JPnBoY/8UwsBmRKcUDLV37YW2jlksRS+k0vhHBfnQHE3tynSAoAjSgw62fjUFVLR uxMOO3jmUnREc1Ipi9yQUvLn3LSMBiGTzYdHcxoUoCS+P744E2LSA3w/7AS/Pcc10x89 4mWBahhwXObNrHSO7n1iauqdjpQ2ToaWhPHYo5bjV99G9A0/qaM+mqwTXWL6bIBVe6d8 K63POCp75/XlcgQFLlybimKPuZuaD1IxZR9LUBtMp3D1K37b41vC7slpRqGBGQw+DZKx 7FvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712563189; x=1713167989; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=PbG/Cv7DGpt7tAVi1gqaOi//Mh/4FAflEt7ny+NUGq4=; b=q+Imo9EjnJ+8NZ05yYOIiFzsbPNXsK+hhoXjv1QHB1Dq+7ttcsj8dsrOqIdtvdwEyM ytmlH/+Y4cjpfMYAbCsl+6xYv9X36arnv5cqQfsnxF6Lm9NEWOUFSQ6WfJNmLGy7miWT HQj5SUL0SFNAvCvooGHymVG6TSTD9ASKXj1P4iVJ+2hiGfkvGp4rQL1OCwRSUzyw+w+h GroGMgIArPTRU3PSiEvz6zZmY4h0IyX5dBm/or8Ill8Oz/93z0qxNmD17QaQjbRArlkj QcfW2FQCF1pxySAoKxnadS18L5fy8lTZQfEhuPPkfXaLvNacWlMT7tYdXz7h2tAqP2Ok dJBQ== X-Forwarded-Encrypted: i=1; AJvYcCWQQCCNKFiofvkg9qXBr62R+Jr1B8EprofOabf2pVPiiWe5U1LZnpHQFjTOCsoqLivKVpKcFimy4oNIee5871QXpqshJew0zltp07OT X-Gm-Message-State: AOJu0Yzj2kWYtXDGX7NBxzqgHJJLCpWbw3H7IHYf/6Ae7JqAp1M2vwgg ZVvWYygizVzfVBJkXbV3vlLrlVEn5mnRdrCCPtKCuTwdqfV1JfXpwPUNMdStoSLub2aIMWBSkVc JXxc4ip7Bqfq+0fgLZkIUHjm5w08FxFQ8u27CfHBYQXGZX6GKcA== X-Received: by 2002:a05:6122:685:b0:4d9:218e:3fdd with SMTP id n5-20020a056122068500b004d9218e3fddmr3695276vkq.1.1712563188580; Mon, 08 Apr 2024 00:59:48 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240402-linked-list-v1-0-b1c59ba7ae3b@google.com> <20240402-linked-list-v1-8-b1c59ba7ae3b@google.com> <18698be5-fe4b-44ed-a12e-444ebffd4b32@proton.me> In-Reply-To: <18698be5-fe4b-44ed-a12e-444ebffd4b32@proton.me> From: Alice Ryhl Date: Mon, 8 Apr 2024 09:59:37 +0200 Message-ID: Subject: Re: [PATCH 8/9] rust: list: support heterogeneous lists To: Benno Lossin Cc: Miguel Ojeda , Andrew Morton , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?UTF-8?Q?Bj=C3=B6rn_Roy_Baron?= , Andreas Hindborg , Marco Elver , Kees Cook , Coly Li , Paolo Abeni , Pierre Gondois , Ingo Molnar , Jakub Kicinski , Wei Yang , Matthew Wilcox , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thu, Apr 4, 2024 at 5:35=E2=80=AFPM Benno Lossin wrote: > > On 02.04.24 14:17, Alice Ryhl wrote: > > @@ -180,6 +184,41 @@ unsafe fn from_fields(me: *mut ListLinksFields) ->= *mut Self { > > } > > } > > > > +/// Similar to [`ListLinks`], but also contains a pointer to the full = value. > > +/// > > +/// This type can be used instead of [`ListLinks`] to support lists wi= th trait objects. > > +#[repr(C)] > > +pub struct ListLinksSelfPtr { > > + /// The `ListLinks` field inside this value. > > + /// > > + /// This is public so that it can be used with `impl_has_list_link= s!`. > > + pub inner: ListLinks, > > + self_ptr: UnsafeCell>, > > +} > > + > > +unsafe impl Send for ListLinksSelfPtr= {} > > +unsafe impl Sync for ListLinksSelfPtr= {} > > Missing SAFETY comments. Will do. > > + > > +impl ListLinksSelfPtr { > > + /// The offset from the [`ListLinks`] to the self pointer field. > > + pub const LIST_LINKS_SELF_PTR_OFFSET: usize =3D core::mem::offset_= of!(Self, self_ptr); > > + > > + /// Creates a new initializer for this type. > > + pub fn new() -> impl PinInit { > > + // INVARIANT: Pin-init initializers can't be used on an existi= ng `Arc`, so this value will > > + // not be constructed in an `Arc` that already has a `ListArc`= . > > + Self { > > + inner: ListLinks { > > + inner: Opaque::new(ListLinksFields { > > + prev: ptr::null_mut(), > > + next: ptr::null_mut(), > > + }), > > + }, > > Why don't you use `inner <- ListLinks::new(),`? Because I wasn't using the macro at all. I was just using the fact that T implements PinInit. But as discussed on another patch, I'll replace this entire method with init::zeroed(). > > + self_ptr: UnsafeCell::new(MaybeUninit::zeroed()), > > + } > > + } > > +} > > + > > impl, const ID: u64> List { > > /// Creates a new empty list. > > pub const fn new() -> Self { > > [...] > > > @@ -94,5 +137,45 @@ unsafe fn post_remove(me: *mut ListLinks<$num>) -> = *const Self { > > } > > } > > }; > > + > > + ( > > + impl$({$($generics:tt)*})? ListItem<$num:tt> for $t:ty { > > + using ListLinksSelfPtr; > > + } $($rest:tt)* > > + ) =3D> { > > + unsafe impl$(<$($generics)*>)? ListItem<$num> for $t { > > + unsafe fn prepare_to_insert(me: *const Self) -> *mut ListL= inks<$num> { > > + let links_field =3D unsafe { Self::view_links(me) }; > > + > > + let spoff =3D ListLinksSelfPtr::::LIST_LIN= KS_SELF_PTR_OFFSET; > > + let self_ptr =3D unsafe { (links_field as *const u8).a= dd(spoff) > > + as *const ::core::cell::UnsafeCell<*const Self> }; > > + let cell_inner =3D ::core::cell::UnsafeCell::raw_get(s= elf_ptr); > > + > > + unsafe { ::core::ptr::write(cell_inner, me) }; > > + links_field > > + } > > + > > + unsafe fn view_links(me: *const Self) -> *mut ListLinks<$n= um> { > > + unsafe { > > + >::raw_get_list_links(m= e.cast_mut()) > > + } > > + } > > + > > + unsafe fn view_value(links_field: *mut ListLinks<$num>) ->= *const Self { > > + let spoff =3D ListLinksSelfPtr::::LIST_LIN= KS_SELF_PTR_OFFSET; > > + let self_ptr =3D unsafe { (links_field as *const u8).a= dd(spoff) > > + as *const ::core::cell::UnsafeCell<*const Self> }; > > + let cell_inner =3D ::core::cell::UnsafeCell::raw_get(s= elf_ptr); > > + unsafe { > > + ::core::ptr::read(cell_inner) > > + } > > + } > > + > > + unsafe fn post_remove(me: *mut ListLinks<$num>) -> *const = Self { > > + unsafe { Self::view_value(me) } > > + } > > + } > > + }; > > The paths in this macro should use `$crate::...` to prevent import > errors. Will do. Alice