Received: by 2002:ab2:7a55:0:b0:1f4:4a7d:290d with SMTP id u21csp64354lqp; Thu, 4 Apr 2024 07:14:38 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUVS64fFF4IV0gQP1JynyBv+ePlVj6XGzpRXDpztaXC/e6Xy9+L+AJXu9RvpUYjcSCr1P7sVvFgsVG0Fa7LEi/mCUjBL3sEeBJYakOIWg== X-Google-Smtp-Source: AGHT+IHxQ2bpEklVu/be0vQCfdGB/KqMrFPD/UFUfOe6wvyIk4/2s7btzrlcbhnmPafiALBkF7CZ X-Received: by 2002:a05:6358:7e01:b0:183:6427:10b6 with SMTP id o1-20020a0563587e0100b00183642710b6mr2683421rwm.19.1712240078379; Thu, 04 Apr 2024 07:14:38 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712240078; cv=pass; d=google.com; s=arc-20160816; b=NpBG0qmuSArLDIeyJVayFQ2Zn8kUJs4zUdq6sKljO2fCuR6QfjHGzn0UPDWXTotGeR N2LwczvUcH9Htuxeu6JJWuOdGqbJCFuPGPxNVtE57VyOxPrvAH4If371OAk1HhvehJSN oRGHbjAoHlPzdPGRWTkmUsWiqdSrQpfgzGVKAz6UqV9WDEDg9sPJIUesbSKC0JRBgKdy 9Wkp1+qkBQKqvpHmZAQqp3Ysd4b9Iu61TRJhq6KbSPGG+Vw9Gyh3/goVgYue1scUesvf 1lof4LXAuf5hN583Dh0POEu9Nnetu8aOs4VydSFBCsFGME+9814SrOgdU16hHt5eoE41 DrPQ== 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=rSMg4gwe2BY9lOXwY09BxCvuJ/qllvCDdUS42+hf1tc=; fh=2+mLgF74ksXZh9FItvecyLqM0xHnljaZWsSyEzwTmxU=; b=szWpJ2pflH7G2X5hG4t/skHSeoillxAFq3uen0TRDbKNAu2fXOmNTpsBLLY23cqT5O dWm0qK1Tyo9n8sQlop1DYopYuNSz9/h4Z9tybOVJBsShfqMNLOyFD4W/dXJAf5bemhVP jLaniuWRICrzqs4wwpxCJNvTTJI9AezKea/scSWTv0XkeP4ZXa3r3T97IGnTiCWGr79n 5VDvqgfwsbfLxHIrCA9RBjUU6q05hoUKjIIEZ5tmkqaaPGMWxTc2C05mwm/BkVPYG2Im 2ti0KQwjNZxKVBP+A3txAtqGUps1dt12uYed3nS+XyFBm4ZsSAGSKBM6p11xJrvs70eH 8pjg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=NCURPiOn; 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-131573-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-131573-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id c10-20020a63350a000000b005d8e320fea9si14889110pga.112.2024.04.04.07.14.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Apr 2024 07:14:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-131573-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=NCURPiOn; 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-131573-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-131573-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 19F1F29114C for ; Thu, 4 Apr 2024 14:12:39 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 07483129E66; Thu, 4 Apr 2024 14:12:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="NCURPiOn" Received: from mail-oi1-f177.google.com (mail-oi1-f177.google.com [209.85.167.177]) (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 E79646CDDC for ; Thu, 4 Apr 2024 14:12:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.177 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712239951; cv=none; b=ogf+51XDWXNRkLNQ+RsFP35PVxMvrNnExUD3uPIJZBOmIX25Hb2TSKMwJe+SfIYQmYhWK+5ypdRwJXdHtgW1xXfdh6cKWvtn7QDQJexPZw0Ko4zZywxpPHv0Z/0PmmZvrRhuc42dBEQvdE8b2hn2dNpzcK4J5gE5cP3YoczIuas= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712239951; c=relaxed/simple; bh=d5N7PMJWMpyiLE+xyvBaoDJlSOen3MFajb3KY9fJFy8=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=OyoA+j1nSuoiMzbKl7bjtfj9D/grj3Qa/hvQkYRONeSRT2ED4O7sSOom1PiVlQcerg8oCEUntiqb6V8rBT2xPgw8N86+iyExq373YvJG8nCAzFDRixAdDiZyv8aKPRVFSg6oyyAGbFTWCahGxSQ8vmDIzDwlRnNgyq1WEDkIVZs= 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=NCURPiOn; arc=none smtp.client-ip=209.85.167.177 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-oi1-f177.google.com with SMTP id 5614622812f47-3c4f23f3eeaso281067b6e.1 for ; Thu, 04 Apr 2024 07:12:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1712239948; x=1712844748; 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=rSMg4gwe2BY9lOXwY09BxCvuJ/qllvCDdUS42+hf1tc=; b=NCURPiOnoT7bx7HWdl2m1Tzd9RqwQN8TsQlgcCUPUFW8DOoWadji9bmUAT6ycZPftv SSy9BjMPcNHbizffGXajwOd7oDBTAleH0CT6WCsWO0Ylc6/ymkCm1UdYLJLit2y9FqQ8 wG/jSmCEFooeSYbvofgJdc7RT2B+QRy6mTtv/re/iHwHwd/cI1wycktdS6z2PBC1VFh1 U8AGnOpki92fyjkMVnyP8QLC7kALUR/QS1JhFW3le6DFUa0477JqKLHpyyZdjb+cQn2s 47tuCbf/pV+t7ZqY0wGNiuFT949eG3rCYuyhyd1F3WJ+K+a6FPXOKB2GtpByCFDau2NZ Rz/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712239948; x=1712844748; 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=rSMg4gwe2BY9lOXwY09BxCvuJ/qllvCDdUS42+hf1tc=; b=czu13xSPVoIPb/j+u/YqEfsUdrvlG+1FaZPZuZ8O+uoBt7o+/0CqzosxJ5Uhm+kt6C QJ9FFyfjsT7/Q6AHflDhu9XagX+310jCtDP0MswWyCG3lgGe1mdNwgTHDOvkOdZtN8Wi R8qnvzjWGxX7q7mEr4rntHYFuqJ7n+wpOWFKc8U/j1Ml4mmyksoQeKxDmwctu/6wIeJP QXw+A3EMjolcitLsXbkcLSpkkGQ9MNgpUMQBwMwko8tYv83EYNAw8BUyivQRIYFN5uf5 /f5Yc6LI1m4A9F4DELluYpGNGCNJTsMDxj6s1irgufgMRtrBhxhxcBu+zmaBZeOlZonG +3Cw== X-Forwarded-Encrypted: i=1; AJvYcCV0oUgic6hAQjNPGJh7nPuYEFQMvJFDJikiankXc54PAWG/PBLt68PgD1lbqmO5/BhlJfirVS93OgFfjPiP4D/BHO7MgbKfLPNnv7qq X-Gm-Message-State: AOJu0Yy3L0vzRxGLV5z8qYp5AAwd7bv1HcC8XOWd4DcVUYCZW7aPdAwO y2WahDR7f2zGH3uN1ZnpIY9x1L5BcMORr25NS3ZSBjhMzxeoy7hld/xOvt/TprCxjrvhP4Y4UjX 8qw2+hM1sFiMfufRiX9kmvdshBrQ0BBEy8WlT X-Received: by 2002:a05:6808:11ca:b0:3c3:f471:cdb2 with SMTP id p10-20020a05680811ca00b003c3f471cdb2mr3016608oiv.12.1712239947283; Thu, 04 Apr 2024 07:12:27 -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-5-b1c59ba7ae3b@google.com> In-Reply-To: From: Alice Ryhl Date: Thu, 4 Apr 2024 16:12:16 +0200 Message-ID: Subject: Re: [PATCH 5/9] rust: list: add List 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 4:03=E2=80=AFPM Benno Lossin wrote: > > On 02.04.24 14:17, Alice Ryhl wrote: > > Add the actual linked list itself. > > > > The linked list uses the following design: The List type itself just ha= s > > a single pointer to the first element of the list. And the actual list > > items then form a cycle. So the last item is `first->prev`. > > > > This is slightly different from the usual kernel linked list. Matching > > that exactly would amount to giving List two pointers, and having it be > > part of the cycle of items. This alternate design has the advantage tha= t > > the cycle is never completely empty, which can reduce the number of > > branches in some cases. However, it also has the disadvantage that List > > must be pinned, which this design is trying to avoid. > > > > Having the list items form a cycle rather than having null pointers at > > the beginning/end is convenient for several reasons. For one, it lets u= s > > store only one pointer in List, and it simplifies the implementation of > > several functions. > > > > Unfortunately, the `remove` function that removes an arbitrary element > > from the list has to be unsafe. This is needed because there is no way > > to handle the case where you pass an element from the wrong list. For > > example, if it is the first element of some other list, then that other > > list's `first` pointer would not be updated. Similarly, it could be a > > data race if you try to remove it from two different lists in parallel. > > (There's no problem with passing `remove` an item that's not in any > > list. Additionally, other removal methods such as `pop_front` need not > > be unsafe, as they can't be used to remove items from another list.) > > > > Signed-off-by: Alice Ryhl > > --- > > rust/kernel/list.rs | 294 +++++++++++++++++++++++++++++++++++++++= ++++++++- > > rust/kernel/list/arc.rs | 6 +- > > 2 files changed, 295 insertions(+), 5 deletions(-) > > > > diff --git a/rust/kernel/list.rs b/rust/kernel/list.rs > > index 7af5109500f2..7e9ed802b26b 100644 > > --- a/rust/kernel/list.rs > > +++ b/rust/kernel/list.rs > > @@ -6,6 +6,7 @@ > > > > use crate::init::PinInit; > > use crate::types::Opaque; > > +use core::marker::PhantomData; > > use core::ptr; > > > > mod impl_list_item_mod; > > @@ -16,7 +17,41 @@ > > impl_list_arc_safe, AtomicListArcTracker, ListArc, ListArcSafe, T= ryNewListArc, > > }; > > > > -/// Implemented by types where a [`ListArc`] can be inserted int= o a `List`. > > +/// A linked list. > > +/// > > +/// All elements in this linked list will be [`ListArc`] references to= the value. Since a value can > > +/// only have one `ListArc` (for each pair of prev/next pointers), thi= s ensures that the same > > +/// prev/next pointers are not used for several linked lists. > > +/// > > +/// # Invariants > > +/// > > +/// If the list is empty, then `first` is null. Otherwise, it points a= t the links field of the > > +/// first element of this list. The prev/next pointers of items in the= list will always form a > > +/// cycle. This means that prev/next pointers for an item in a list ar= e never null and never > > +/// dangling. > > I think this is missing that all elements of the list are in `ListArc`s. > > About "This means that prev/next pointers for an item in a list are > never null and never dangling.", I think it would be simpler to say that > "All prev/next pointers of items in the list are valid and form a cycle." Sure. > > +pub struct List, const ID: u64 =3D 0> { > > + first: *mut ListLinksFields, > > + _ty: PhantomData>, > > +} > > + > > +// SAFETY: This is a container of `ListArc`, and access to the = container allows the same > > +// type of access to the `ListArc` elements. > > +unsafe impl Send for List > > +where > > + ListArc: Send, > > + T: ?Sized + ListItem, > > +{ > > +} > > +// SAFETY: This is a container of `ListArc`, and access to the = container allows the same > > +// type of access to the `ListArc` elements. > > +unsafe impl Sync for List > > +where > > + ListArc: Sync, > > + T: ?Sized + ListItem, > > +{ > > +} > > + > > +/// Implemented by types where a [`ListArc`] can be inserted int= o a [`List`]. > > /// > > /// # Safety > > /// > > @@ -56,7 +91,7 @@ pub unsafe trait ListItem: ListA= rcSafe { > > /// been called. > > unsafe fn view_value(me: *mut ListLinks) -> *const Self; > > > > - /// This is called when an item is inserted into a `List`. > > + /// This is called when an item is inserted into a [`List`]. > > /// > > /// # Guarantees > > /// > > @@ -103,7 +138,6 @@ struct ListLinksFields { > > /// The fields are null if and only if this item is not in a list. > > #[repr(transparent)] > > pub struct ListLinks { > > - #[allow(dead_code)] > > inner: Opaque, > > } > > > > @@ -125,4 +159,258 @@ pub fn new() -> impl PinInit { > > }), > > } > > } > > + > > + /// # Safety > > + /// > > + /// The pointer must be dereferencable. > > I would use "`me` is valid.". Ok. > > + #[inline] > > + unsafe fn fields(me: *mut Self) -> *mut ListLinksFields { > > + // SAFETY: The caller promises that the pointer is valid. > > + unsafe { Opaque::raw_get(ptr::addr_of!((*me).inner)) } > > + } > > + > > + /// # Safety > > + /// > > + /// The pointer must be dereferencable. > > Ditto. Ok. > > + #[inline] > > + unsafe fn from_fields(me: *mut ListLinksFields) -> *mut Self { > > + me.cast() > > + } > > +} > > + > > +impl, const ID: u64> List { > > + /// Creates a new empty list. > > + pub const fn new() -> Self { > > + Self { > > + first: ptr::null_mut(), > > + _ty: PhantomData, > > + } > > + } > > + > > + /// Returns whether this list is empty. > > + pub fn is_empty(&self) -> bool { > > + self.first.is_null() > > + } > > + > > + /// Add the provided item to the back of the list. > > + pub fn push_back(&mut self, item: ListArc) { > > + let item =3D unsafe { ListLinks::fields(T::prepare_to_insert(L= istArc::into_raw(item))) }; > > Missing SAFETY comment. Will add one here and in push_front. > > + > > + if self.first.is_null() { > > + self.first =3D item; > > + // SAFETY: The caller just gave us ownership of these fiel= ds. > > + // INVARIANT: A linked list with one item should be cyclic= . > > + unsafe { > > + (*item).next =3D item; > > + (*item).prev =3D item; > > + } > > + } else { > > + let next =3D self.first; > > + // SAFETY: We just checked that `next` is non-null. > > Missing mention of the type invariant. SAFETY: By the type invariant, this pointer is valid or null. We just checked that it's not null, so it must be valid. > > + let prev =3D unsafe { (*next).prev }; > > + // SAFETY: Pointers in a linked list are never dangling, a= nd the caller just gave us > > + // ownership of the fields on `item`. > > + // INVARIANT: This correctly inserts `item` between `prev`= and `next`. > > + unsafe { > > + (*item).next =3D next; > > + (*item).prev =3D prev; > > + (*prev).next =3D item; > > + (*next).prev =3D item; > > + } > > + } > > + } > > + > > + /// Add the provided item to the front of the list. > > + pub fn push_front(&mut self, item: ListArc) { > > + let item =3D unsafe { ListLinks::fields(T::prepare_to_insert(L= istArc::into_raw(item))) }; > > + > > + if self.first.is_null() { > > + // SAFETY: The caller just gave us ownership of these fiel= ds. > > + // INVARIANT: A linked list with one item should be cyclic= . > > + unsafe { > > + (*item).next =3D item; > > + (*item).prev =3D item; > > + } > > + } else { > > + let next =3D self.first; > > + // SAFETY: We just checked that `next` is non-null. > > + let prev =3D unsafe { (*next).prev }; > > + // SAFETY: Pointers in a linked list are never dangling, a= nd the caller just gave us > > + // ownership of the fields on `item`. > > + // INVARIANT: This correctly inserts `item` between `prev`= and `next`. > > + unsafe { > > + (*item).next =3D next; > > + (*item).prev =3D prev; > > + (*prev).next =3D item; > > + (*next).prev =3D item; > > + } > > + } > > + self.first =3D item; > > + } > > + > > + /// Removes the last item from this list. > > + pub fn pop_back(&mut self) -> Option> { > > + if self.first.is_null() { > > + return None; > > + } > > + > > + // SAFETY: We just checked that the list is not empty. > > + let last =3D unsafe { (*self.first).prev }; > > + // SAFETY: The last item of this list is in this list. > > + Some(unsafe { self.remove_internal(last) }) > > + } > > + > > + /// Removes the first item from this list. > > + pub fn pop_front(&mut self) -> Option> { > > + if self.first.is_null() { > > + return None; > > + } > > + > > + // SAFETY: The first item of this list is in this list. > > + Some(unsafe { self.remove_internal(self.first) }) > > + } > > + > > + /// Removes the provided item from this list and returns it. > > + /// > > + /// This returns `None` if the item is not in the list. > > I think this should say "Returns `None` if the item is not in a list.". > (Technically it should be "is not in a `List`", since it *can* be > in another list with a different ID.) I'm not really convinced. The phrases "the list" and "a list" are equivalent given the safety requirement for this method, but "the list" seems more natural to me. The `remove` method of any other collection would say "the list" too. > > + /// > > + /// # Safety > > + /// > > + /// The provided item must not be in a different linked list. > > + pub unsafe fn remove(&mut self, item: &T) -> Option= > { > > + let mut item =3D unsafe { ListLinks::fields(T::view_links(item= )) }; > > + // SAFETY: The user provided a reference, and reference are ne= ver dangling. > > + // > > + // As for why this is not a data race, there are two cases: > > + // > > + // * If `item` is not in any list, then these fields are read= -only and null. > > + // * If `item` is in this list, then we have exclusive access= to these fields since we > > + // have a mutable reference to the list. > > + // > > + // In either case, there's no race. > > + let ListLinksFields { next, prev } =3D unsafe { *item }; > > + > > + debug_assert_eq!(next.is_null(), prev.is_null()); > > + if !next.is_null() { > > + // This is really a no-op, but this ensures that `item` is= a raw pointer that was > > + // obtained without going through a pointer->reference->po= inter conversion rountrip. > > + // This ensures that the list is valid under the more rest= rictive strict provenance > > + // ruleset. > > + // > > + // SAFETY: We just checked that `next` is not null, and it= 's not dangling by the > > + // list invariants. > > + unsafe { > > + debug_assert_eq!(item, (*next).prev); > > + item =3D (*next).prev; > > + } > > + > > + // SAFETY: We just checked that `item` is in a list, so th= e caller guarantees that it > > + // is in this list. The pointers are in the right order. > > + Some(unsafe { self.remove_internal_inner(item, next, prev)= }) > > + } else { > > + None > > + } > > + } > > + > > + /// Removes the provided item from the list. > > + /// > > + /// # Safety > > + /// > > + /// The pointer must point at an item in this list. > > + unsafe fn remove_internal(&mut self, item: *mut ListLinksFields) -= > ListArc { > > + // SAFETY: The caller promises that this pointer is not dangli= ng, and there's no data race > > + // since we have a mutable reference to the list containing `i= tem`. > > + let ListLinksFields { next, prev } =3D unsafe { *item }; > > + // SAFETY: The pointers are ok and in the right order. > > + unsafe { self.remove_internal_inner(item, next, prev) } > > + } > > + > > + /// Removes the provided item from the list. > > + /// > > + /// # Safety > > + /// > > + /// The `item` pointer must point at an item in this list, and we = must have `(*item).next =3D=3D > > + /// next` and `(*item).prev =3D=3D prev`. > > + unsafe fn remove_internal_inner( > > + &mut self, > > + item: *mut ListLinksFields, > > + next: *mut ListLinksFields, > > + prev: *mut ListLinksFields, > > + ) -> ListArc { > > + // SAFETY: We have exclusive access to items in the list, and = prev/next pointers are > > I think you mean that you have exclusive access to the prev/next fields > of the `ListLinks` associated with `ID`... But that is rather long. > Does anyone have any idea to shorten this? SAFETY: We have exclusive access to the pointers of items in the list, and the prev/next pointers are never null for items in a list. > > + // never null for items in a list. > > + // > > + // INVARIANT: There are three cases: > > + // * If the list has at least three items, then after removin= g the item, `prev` and `next` > > + // will be next to each other. > > + // * If the list has two items, then the remaining item will = point at itself. > > + // * If the list has one item, then `next =3D=3D prev =3D=3D = item`, so these writes have no effect > > + // due to the writes to `item` below. > > I think the writes do not have an effect. (no need to reference the > writes to `item` below) ? > > + unsafe { > > + (*next).prev =3D prev; > > + (*prev).next =3D next; > > + } > > + // SAFETY: We have exclusive access to items in the list. > > + // INVARIANT: The item is no longer in a list, so the pointers= should be null. > > + unsafe { > > + (*item).prev =3D ptr::null_mut(); > > + (*item).next =3D ptr::null_mut(); > > + } > > + // INVARIANT: There are three cases: > > + // * If `item` was not the first item, then `self.first` shou= ld remain unchanged. > > + // * If `item` was the first item and there is another item, = then we just updated > > + // `prev->next` to `next`, which is the new first item, and= setting `item->next` to null > > + // did not modify `prev->next`. > > + // * If `item` was the only item in the list, then `prev =3D= =3D item`, and we just set > > + // `item->next` to null, so this correctly sets `first` to = null now that the list is > > + // empty. > > + if self.first =3D=3D item { > > + // SAFETY: The `prev` field of an item in a list is never = dangling. > > I don't think this SAFETY comment makes sense. > > > + self.first =3D unsafe { (*prev).next }; SAFETY: `prev` is the `prev` pointer from a `ListLinks` in a `List`, so the pointer is valid. There's no race, since we have exclusive access to the list. Alice