Received: by 2002:ab2:1149:0:b0:1f3:1f8c:d0c6 with SMTP id z9csp2797332lqz; Wed, 3 Apr 2024 08:53:24 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVwB1ZdmsOvCHgWVJy4D4XdyOHSzNcsLSN1tMgFQufZDcsB+YoqpCxsMwpCUoYQBJ3WIxpYpZ4sGYQVlfMVDzhHkNWH9w2TNKsVewcjKg== X-Google-Smtp-Source: AGHT+IH6zzrgVgrnPLL2LbCsBiEl4L5TVatkYG3wVg8RaYzIWaw3Sk3iU1CGgusAOiRRT9OTrq1B X-Received: by 2002:a19:2d46:0:b0:516:c3ee:5c79 with SMTP id t6-20020a192d46000000b00516c3ee5c79mr314089lft.37.1712159603351; Wed, 03 Apr 2024 08:53:23 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712159603; cv=pass; d=google.com; s=arc-20160816; b=0cW29jMuX86HY0dPE6qTUWvps2IvJH+D65KWwnAts3W+ZDidjXRqyLJn0dTTNBdZEH cRmdX8fGn2DEPCSAyxTBiQUBk5jzTfikfg1ZZzxVTX006SNXy+sa4hXxTALDL3FAaNkK f9/OAcIpSykucwREzjJzEc8lD76GAV4V94dXP+zAQtzO+obX8K2ZymXkzVjebQzXumWh E/hnFlxWe57xtG9KNC04Zhz83/0rohni8M67kxUe+UAdWlUx+z/Fac5rQG3s66WVeK3q ncU7300zGOZRocZBB/C9vVSSAU3wegLC3lGjOFuyWXCBDqVjr8VO1eMkXp2oyu2vqNcV 8j3Q== 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=UW23mqUm97ijKHcnsCqAhKuKRDp8kBhzeRwm31J4K/E=; fh=1eN+strsLnX8cuvE+uTff7wY1KiEAf1jFO97IDCVf3U=; b=eAVxEI4EMtyZ9t6Agg68GcaGu1VX5HnOCndExbkKkZ1xFmPOJN7gQDt0hWraZU5y/8 lJJ4w0kzrzc+42vxIguKtWkB9zRDqfjhjLd1k0Y4+Lk7sQcFDdTGktCTo5hONMI5Nza9 iId/5o+mXCbWfbBhJwkyXZPkTYB7K6S8sAmBQeN3fW5qrJFLK9oeZCKm4wvvjEknug42 iAAYXyFNT4cWy+kXCTlC8Ary0r+aKc7UAUauBaOBa1kLRSD4W8q9rYcwFnfLj7HK/+9Q tt0HGzprBWGQbM0ir/hFacurn5QEcMLXguCRdimLpv6ZHz5AeYirYiORJ/M+vu9EppBX FL3g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=a4SGYtwi; 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-130136-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-130136-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id 1-20020a170906310100b00a474da2177esi6929882ejx.532.2024.04.03.08.53.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 08:53:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-130136-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=a4SGYtwi; 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-130136-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-130136-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 8D1551F20F78 for ; Wed, 3 Apr 2024 15:52:15 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C4A4314A63E; Wed, 3 Apr 2024 15:51:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b="a4SGYtwi" Received: from mail-40134.protonmail.ch (mail-40134.protonmail.ch [185.70.40.134]) (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 9222D146A96; Wed, 3 Apr 2024 15:51:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.70.40.134 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712159514; cv=none; b=Sk+apQFCpyUn4esQVTpwW6glmiTTdM7w0cIZeNYP7Epljnj4jlVeyV8ZUaHpotBL55ogAVlQHin2kmGCfKuniwM32jWvZ5JBny6Lyv8vPe06bT1ppwV5OVP4mQHP7GVxe9ShZ1NTKpyWIpfZPTlOcPZZbmsDFrvnFaKIgT9Ypxc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712159514; c=relaxed/simple; bh=wOtVDkDP5TIIgmnuGlntttCWA3GnSxnAWVJEuBaypkw=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=OtiHsKHTSsX8k1Jseap+FrpOZXuKg64+TjfjPMeMggafC0o/Q27ZBUmLKa49GtMP++RTJUQVGDOBYY66Wmz3UwFsr2hskkHE+UJOYYE9gU98sB5LuGxQrjew70dBIzlZ4HU6LGfBMXau/U9jKk8hOdCTlL2XV+vJ2bUvHJj++24= 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=a4SGYtwi; arc=none smtp.client-ip=185.70.40.134 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=1712159508; x=1712418708; bh=UW23mqUm97ijKHcnsCqAhKuKRDp8kBhzeRwm31J4K/E=; 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=a4SGYtwiY6BXaeQNZkqHurT+AkVChaN1RMlIRu+oPuTJ9UvfV22vAs953oSyDuVgo sSsk9CS4xQlq2z3RrXf/aXnP9djkNLaWtS4+z5h7nVkfQTgRwcfBGe4loJiMs7z74S DE8/LPB5jTFt9BR/xhCAnR3D1l4GNSWbjG74PLFw7ZIpPXNj/9i5uIkok+rFwJO0XU ODnThcf8N/S3rVxPO3qTRg5A9DTpb0ZwpOfzWXrqMdbet1Ztwf8h8JGcs3OD7orzWy CksfvKS64XwVcBpwheqG6CRfqBshxTK1eA+vkcsYNTImlnaJRzoxI6GxbtDLzOX+t8 clMNPE9Zom7iQ== Date: Wed, 03 Apr 2024 15:51:42 +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 1/9] rust: list: add ListArc Message-ID: <2f25f21e-fad8-48bb-aa2b-d61bf8909a41@proton.me> In-Reply-To: <20240402-linked-list-v1-1-b1c59ba7ae3b@google.com> References: <20240402-linked-list-v1-0-b1c59ba7ae3b@google.com> <20240402-linked-list-v1-1-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:16, Alice Ryhl wrote: > +impl, const ID: u64> ListArc { > + /// Constructs a new reference counted instance of `T`. > + pub fn try_new(contents: T) -> Result { > + Ok(Self::from_unique(UniqueArc::try_new(contents)?)) > + } > + > + /// Use the given initializer to in-place initialize a `T`. > + /// > + /// If `T: !Unpin` it will not be able to move afterwards. > + pub fn pin_init(init: impl PinInit) -> error::Result > + where > + Error: From, > + { > + Ok(Self::from_pin_unique(UniqueArc::pin_init(init)?)) > + } pin-init has a general trait for this: InPlaceInit. I don't know if the other functions that it provides would help you. > +} > + > +impl ListArc > +where > + T: ListArcSafe + ?Sized, > +{ > + /// Convert a [`UniqueArc`] into a [`ListArc`]. > + pub fn from_unique(mut unique: UniqueArc) -> Self { > + // SAFETY: We have a `UniqueArc`, so there is no `ListArc`. > + unsafe { T::on_create_list_arc_from_unique(&mut unique) }; > + let arc =3D Arc::from(unique); > + // SAFETY: We just called `on_create_list_arc_from_unique` on an= arc without a `ListArc`, > + // so we can create a `ListArc`. > + unsafe { Self::transmute_from_arc(arc) } > + } > + > + /// Convert a pinned [`UniqueArc`] into a [`ListArc`]. > + pub fn from_pin_unique(unique: Pin>) -> Self { > + // SAFETY: We continue to treat this pointer as pinned after thi= s call, since `ListArc` > + // implicitly pins its value. This is not sufficient, since you also rely on `Self::from_unique` to handle the parameter as if it were pinned, which it does not. Since it calls `T::on_create_list_arc_from_unique` which just gets a `&mut self` as a parameter and it could move stuff out. > + Self::from_unique(unsafe { Pin::into_inner_unchecked(unique) }) > + } > + > + /// Like [`from_unique`], but creates two `ListArcs`. > + /// > + /// The two ids must be different. > + /// > + /// [`from_unique`]: ListArc::from_unique > + pub fn pair_from_unique(mut unique: UniqueArc) ->= (Self, ListArc) > + where > + T: ListArcSafe, > + { > + assert_ne!(ID, ID2); Can this be a `build_assert!`? > + > + // SAFETY: We have a `UniqueArc`, so we can call this method. I liked the comment from above better: // SAFETY: We have a `UniqueArc`, so there is no `ListArc`. Maybe use the variation "so there are no `ListArc`s for any ID.". > + unsafe { >::on_create_list_arc_from_unique(= &mut unique) }; > + // SAFETY: We have a `UniqueArc`, so we can call this method. Th= e two ids are not equal. > + unsafe { >::on_create_list_arc_from_unique= (&mut unique) }; > + > + let arc1 =3D Arc::from(unique); > + let arc2 =3D Arc::clone(&arc1); > + > + // SAFETY: We just called `on_create_list_arc_from_unique` on an= arc without a `ListArc`, > + // so we can create a `ListArc`. I would mention the two different IDs again. > + unsafe { > + ( > + Self::transmute_from_arc(arc1), > + ListArc::transmute_from_arc(arc2), > + ) > + } > + } > + > + /// Like [`pair_from_unique`], but uses a pinned arc. > + /// > + /// The two ids must be different. > + /// > + /// [`pair_from_unique`]: ListArc::pair_from_unique > + pub fn pair_from_pin_unique( > + unique: Pin>, > + ) -> (Self, ListArc) > + where > + T: ListArcSafe, > + { > + // SAFETY: We continue to treat this pointer as pinned after thi= s call, since `ListArc` > + // implicitly pins its value. > + Self::pair_from_unique(unsafe { Pin::into_inner_unchecked(unique= ) }) > + } > + > + /// Transmutes an [`Arc`] into a `ListArc` without updating the trac= king inside `T`. > + /// > + /// # Safety > + /// > + /// * The value must not already have a `ListArc` reference. > + /// * The tracking inside `T` must think that there is a `ListArc` r= eference. > + #[inline] > + unsafe fn transmute_from_arc(me: Arc) -> Self { > + // INVARIANT: By the safety requirements, the invariants on `Lis= tArc` are satisfied. > + // SAFETY: ListArc is repr(transparent). > + unsafe { core::mem::transmute(me) } Why do you need a transmute here? Can't you just construct the struct? Self { arc: me } --=20 Cheers, Benno > + } > +