Received: by 2002:ab2:7a55:0:b0:1f4:4a7d:290d with SMTP id u21csp145241lqp; Thu, 4 Apr 2024 09:07:08 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXb4u7F9v7S0qDB0iFkop5/bigoM1lZlJQv/IRpjb7uZGv9OVHDSDLYxwAws9fgn5Sh9uJuhvMRWhZlulJ+KEWnjLnHiBlL5+okOw7Iug== X-Google-Smtp-Source: AGHT+IFL1pQVkIBM6ksbLxHbWhjF8tHhYYWbyB4+RTuA4sdu6aVBgX0muxnUYHQDbgpngLZRUz3C X-Received: by 2002:a05:6a20:914e:b0:1a7:3498:8d36 with SMTP id x14-20020a056a20914e00b001a734988d36mr299297pzc.17.1712246827759; Thu, 04 Apr 2024 09:07:07 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712246827; cv=pass; d=google.com; s=arc-20160816; b=Gi5Mz99dgfhwi0aBMsEDrNeGuvxWfxzSpyGNMQ1u0831Wv6ZzTGfd5CPbCDbnUoQwd rcPfwYnA5uw/PkHn2IwGwX3aHyZkMtaboUn3kO/W7+opBqalPdLi4M8WPtaDo0Viq+82 3m/5/PN5qhy8eyu2FRf+MP5TmI3ZQt4Rv0TbUyiJEPH9WWmmhMjgjv0QjJII+o1pWZHX cR70HDK/MCWxh5Np/suw3DDRmAOL3/OrwRipCkKKkM/aHL9y9ZSyRRYsugt6yp8j+qMx J11j1aKNH3r9Jd7meAp6sUE8sKgis0JakR6GOkG08+V3BIXlV4w8i1hkTjH3y48rnXUJ afEA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:feedback-id:references :in-reply-to:message-id:subject:cc:from:to:date:dkim-signature; bh=DMdXBi/0LZqIUvhAKHpVgBvC+3bzEmZ+xPRxQLFsuwM=; fh=1eN+strsLnX8cuvE+uTff7wY1KiEAf1jFO97IDCVf3U=; b=A+UF9Qu/sZShWMDiJIO6V5aSOAvVH7b1z5LoWoX4UQKb9HoQFbAilZ38gRCVjM78gq q9pGnE6bxzGC1Dp5SHnuhHsZ7U32hMHlgBAE+y/lkORMRApiAaOzaATDZThlbPuEEers rL0doL/0/LBAnKEYO+1q/NHXE+5UdZ2CK4+ut9HoX7MA4AjySY70qlTw/FkZsPXWxbU8 Pgj/9ncwpMo9PUhoR7W8diCvq9DDo8EAeuRB/Znx8V0vi8qythWRpsO2R13Wg/7xYw71 BVebv5v3pAKvP8bM/dyY2gH0hrsYIaE7bnVkHbjlmTsSBMvIXRnlyTmVSMNaMiis4wjT W9Xg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=kG5CqHgg; arc=pass (i=1 spf=pass spfdomain=proton.me dkim=pass dkdomain=proton.me dmarc=pass fromdomain=proton.me); spf=pass (google.com: domain of linux-kernel+bounces-131736-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-131736-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id lp15-20020a056a003d4f00b006ecf19a3353si1234403pfb.394.2024.04.04.09.07.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Apr 2024 09:07:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-131736-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=kG5CqHgg; arc=pass (i=1 spf=pass spfdomain=proton.me dkim=pass dkdomain=proton.me dmarc=pass fromdomain=proton.me); spf=pass (google.com: domain of linux-kernel+bounces-131736-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-131736-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me 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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 895E6B21D47 for ; Thu, 4 Apr 2024 15:36:13 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D030F12A16C; Thu, 4 Apr 2024 15:36:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b="kG5CqHgg" Received: from mail-4316.protonmail.ch (mail-4316.protonmail.ch [185.70.43.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D2B771292F3; Thu, 4 Apr 2024 15:35:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.70.43.16 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712244961; cv=none; b=RAdIp7ntuYITiedWwEBEIhrTDLpCsPdKYk1YwU/Y3sQl+uo2Awy/qKtD/VONvA/N+XkBJReCDoGdj0Oy0MNSxCfDtszqxmboyDKqzWCDdSHeSCykeHNKKdAwvbb1oJoKjQ6pVpei6MLtDqHIpR5UckcV7i+64zQF0Qflh5hak+g= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712244961; c=relaxed/simple; bh=fYbjEPVccjvYnn46iD7vpnVoAkxI1If0zG6IiW1R9Yc=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=EX+PRgUQw7YpSmwqTOBSy+qF1/juGKzfBp93YWykG4CXus2UpewSMY9z5Sf82SdcfXc3Ojaz8cK9EYPF2LfpJm6GtQFvsUX1wytsnbU9A6/YcpydpfFPcyg7rj/WKtfND06kUMlvSCWzQiSNrN12CZDpawdNEEB3urTsqsWxozQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me; spf=pass smtp.mailfrom=proton.me; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b=kG5CqHgg; arc=none smtp.client-ip=185.70.43.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=proton.me DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1712244956; x=1712504156; bh=DMdXBi/0LZqIUvhAKHpVgBvC+3bzEmZ+xPRxQLFsuwM=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=kG5CqHgg3sWkxpkV4h1CY2VTd2fIjN6bWbDfeKo76m48UFkcTGqap3UjOvpv+WneM 4C+Mpd5Z1Tb0jSdnnLiKCuagArdS+Y/7Q8J0zbBytdw+JqnjCekcdErlq5Me/BQ0TO u2PwF84SRv0suewmB5wHBzXP7471tALVTIo2EVgOj7TH3JVtbD7LdqHMdEwyP6Ndq4 DaNRQ6Jh7sBaAPAnt3hdVAxWBi3K347iiSPSHlelfKULtsJTybwkN/pFR6B+M+E4KT m3jx/FP+aMM7v7CIvcFi9c4TN6Cms2R55sXKDyvdhWZyz5bDpv2yNUkpCAematsTcs DB6GUR9U2HJJw== Date: Thu, 04 Apr 2024 15:35:51 +0000 To: Alice Ryhl , Miguel Ojeda , Andrew Morton From: Benno Lossin Cc: 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 Subject: Re: [PATCH 8/9] rust: list: support heterogeneous lists Message-ID: <18698be5-fe4b-44ed-a12e-444ebffd4b32@proton.me> In-Reply-To: <20240402-linked-list-v1-8-b1c59ba7ae3b@google.com> References: <20240402-linked-list-v1-0-b1c59ba7ae3b@google.com> <20240402-linked-list-v1-8-b1c59ba7ae3b@google.com> Feedback-ID: 71780778:user:proton Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On 02.04.24 14:17, Alice Ryhl wrote: > @@ -180,6 +184,41 @@ unsafe fn from_fields(me: *mut ListLinksFields) -> *= mut Self { > } > } >=20 > +/// Similar to [`ListLinks`], but also contains a pointer to the full va= lue. > +/// > +/// This type can be used instead of [`ListLinks`] to support lists with= 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_links!= `. > + pub inner: ListLinks, > + self_ptr: UnsafeCell>, > +} > + > +unsafe impl Send for ListLinksSelfPtr {} > +unsafe impl Sync for ListLinksSelfPtr {} Missing SAFETY comments. > + > +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 existing= `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(),`? > + 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>) -> *c= onst 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 ListLin= ks<$num> { > + let links_field =3D unsafe { Self::view_links(me) }; > + > + let spoff =3D ListLinksSelfPtr::::LIST_LINKS= _SELF_PTR_OFFSET; > + let self_ptr =3D unsafe { (links_field as *const u8).add= (spoff) > + as *const ::core::cell::UnsafeCell<*const Self> }; > + let cell_inner =3D ::core::cell::UnsafeCell::raw_get(sel= f_ptr); > + > + unsafe { ::core::ptr::write(cell_inner, me) }; > + links_field > + } > + > + unsafe fn view_links(me: *const Self) -> *mut ListLinks<$num= > { > + unsafe { > + >::raw_get_list_links(me.= cast_mut()) > + } > + } > + > + unsafe fn view_value(links_field: *mut ListLinks<$num>) -> *= const Self { > + let spoff =3D ListLinksSelfPtr::::LIST_LINKS= _SELF_PTR_OFFSET; > + let self_ptr =3D unsafe { (links_field as *const u8).add= (spoff) > + as *const ::core::cell::UnsafeCell<*const Self> }; > + let cell_inner =3D ::core::cell::UnsafeCell::raw_get(sel= f_ptr); > + unsafe { > + ::core::ptr::read(cell_inner) > + } > + } > + > + unsafe fn post_remove(me: *mut ListLinks<$num>) -> *const Se= lf { > + unsafe { Self::view_value(me) } > + } > + } > + }; The paths in this macro should use `$crate::...` to prevent import errors. --=20 Cheers, Benno