Received: by 2002:ab2:1149:0:b0:1f3:1f8c:d0c6 with SMTP id z9csp2665692lqz; Wed, 3 Apr 2024 05:20:00 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXfulewpEDtmfDlx6BwyegFJsaoxEQtQBIEl00ARm7w06VVWGjIjOz02bnlEczfgXsSRyCA9v99i8b2NgZXsAxiE+7OggygH7pq59h+YA== X-Google-Smtp-Source: AGHT+IFpK4UZI0lrJeJMkxuG9mtec7esQIGOrTzb+3YVf66k2TvFcetWDrC35gTlCwJmgojGtVTu X-Received: by 2002:a0d:fb82:0:b0:614:2c9b:4c8e with SMTP id l124-20020a0dfb82000000b006142c9b4c8emr14333606ywf.18.1712146799778; Wed, 03 Apr 2024 05:19:59 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712146799; cv=pass; d=google.com; s=arc-20160816; b=uZ0kugQHbUL0J3VGByOZBS9QgbGjCMA0kee/B8+4G0RvOswpUdX5BOSo9fZpT1mfhU 4TYmJBpi7Kb7FTcWTHkpqMP/n6cWVN2eqCyVK381l1HHPOWwV1j2toymlWKttzAGQPXX +J5VQPsXCGNe5VSIP3WSOvMTksrcLvTLUmKsHKAodQ5jgMrsW/JeYMTHsQyTG69VV+Ig ZhoJnMznSN7OhfjIscy1k4qtQmo24viJNWNMdhjWJbZ/JvtPCxQ5Rsjf821PKhGTxjWq q6y3Nz9sXhRseYpz4EqWVumU6EjpoETvp4xDqu/J9y755B6X9tYhHMwkxYF/zL6k2+DJ KRUg== 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=0bM9BhDiM99LAuLM/L3WaFL905yHSxqRHWxKfPusUgY=; fh=1eN+strsLnX8cuvE+uTff7wY1KiEAf1jFO97IDCVf3U=; b=bbIgXuVe3/AwpcDtQbDqLtE5v/fXd5rlXyheCDxHREEzghV/h2bPkODU2QhMCk549X FqNGnVVHsZ2WqZR+exM+MpudinUcBbB1/61hCWMPy2sr5LInqAnQ/OaoaajHcd6dnO2C HANARUWJL364YP/0rxOQYn+725rJj7m1KsOxb/UK5zUv33/kG4cRF2f9vGe4RklB67tX mRbusiyhaJ0DcOu8F/E+cWJcSsl8B2CHbhG0W2kSqPeFWqAxfmpXq88zBOMFjsTyRlkN siZtL/xixuxNaZIHEUFGPhid3Q+3XTz10jKOKs0DZlNqNGu7QTxf/xP0+GUuJTDEmSzv S/IA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=Xv9GRf4L; 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-129711-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-129711-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id y30-20020a05620a0e1e00b0078a624e264bsi13796831qkm.110.2024.04.03.05.19.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 05:19:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-129711-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=Xv9GRf4L; 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-129711-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-129711-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 818C91C23083 for ; Wed, 3 Apr 2024 12:19:59 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id ED2B46E60F; Wed, 3 Apr 2024 12:19:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b="Xv9GRf4L" 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 699DC3DB9B for ; Wed, 3 Apr 2024 12:19:48 +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=1712146791; cv=none; b=E20fbWYeM7lRbrJ0KxRt/SJOlLmXfLoqEdDS4YFqFHSDVLgQJP54zMAHTzfK+2K0WcuFJ2rEN2XQoG7E+cyJvoJBo7nVA7R1ND/KBXZVEE+BPv5fJ/t1IdO9FJJj/8EVUmTAVNxsCvmtQwIOQiDAVbRKYFGPEHV+FEZJF0id8NA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712146791; c=relaxed/simple; bh=DtimUzcCiaEemuoUaPjn4n0jKJP0VTmi4Z8kvystU2g=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=MvIcQobks+qSFRzAzXoU0SnpWQt3NcxflYqFYQzr0V++LW0Z89n+51v7ECpaDQzUuqFc4y+lvaH5Zi9iPE0Y5NAC25fDabyeUY8h0d9TSehHDig0EY95cQdTCnG8lNM5UJrCXTzjfDLe3NRnnHSDY2VSaZz12ojNaWVpXGMtVXk= 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=Xv9GRf4L; 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=1712146786; x=1712405986; bh=0bM9BhDiM99LAuLM/L3WaFL905yHSxqRHWxKfPusUgY=; 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=Xv9GRf4LDL0m+M7PmKZenEhuN4QhZyqARnY2A1tSww0dcPL+oQJ8jDjRdwfmqGkua uNUvQaZGvm7d3Gxt96PKHN6uFes5rb2r79tYvvN5l1BccQRwxRDIt6dy9j0SV3C6N0 VqJysdzsgv7fd68HyQJvlvUkUyXdY/WnSdO/EAzmyFL3dB8NuRRCjMr3ZtYmcbY2ur G6/HsNy1jaJL88pqL5j5IVAv5O+XwQSaip4c9J1Ilmf1J1X7ANn9Caw/8W3KuBGpnw 7Jlakk0IQa8eoubezehHvRZg03s1WXIsRLLzQhd9qXq9a38k4rHafG02GUQqmtHggJ T97UyL1iq3JBw== Date: Wed, 03 Apr 2024 12:19:38 +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 7/9] rust: list: add cursor Message-ID: <4aaf7fd7-49ed-4b3e-b691-0d8d7d426d3a@proton.me> In-Reply-To: <20240402-linked-list-v1-7-b1c59ba7ae3b@google.com> References: <20240402-linked-list-v1-0-b1c59ba7ae3b@google.com> <20240402-linked-list-v1-7-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: > diff --git a/rust/kernel/list.rs b/rust/kernel/list.rs > index 892705dd0571..47e52818c7bd 100644 > --- a/rust/kernel/list.rs > +++ b/rust/kernel/list.rs > @@ -408,6 +408,20 @@ pub fn push_all_back(&mut self, other: &mut List) { > other.first =3D ptr::null_mut(); > } >=20 > + /// Returns a cursor to the first element of the list. > + /// > + /// If the list is empty, this returns `None`. > + pub fn cursor_front(&mut self) -> Option> { > + if self.first.is_null() { > + None > + } else { > + Some(Cursor { Missing INVARIANT comment. > + current: self.first, > + list: self, > + }) > + } > + } > + > /// Creates an iterator over the list. > pub fn iter(&self) -> Iter<'_, T, ID> { > // INVARIANT: If the list is empty, both pointers are null. Othe= rwise, both pointers point > @@ -476,6 +490,69 @@ fn next(&mut self) -> Option> { > } > } >=20 > +/// A cursor into a [`List`]. > +/// > +/// # Invariants > +/// > +/// The `current` pointer points a value in `list`. > +pub struct Cursor<'a, T: ?Sized + ListItem, const ID: u64 =3D 0> { > + current: *mut ListLinksFields, > + list: &'a mut List, > +} > + > +impl<'a, T: ?Sized + ListItem, const ID: u64> Cursor<'a, T, ID> { > + /// Access the current element of this cursor. > + pub fn current(&self) -> ArcBorrow<'_, T> { > + // SAFETY: The `current` pointer points a value in the list. > + let me =3D unsafe { T::view_value(ListLinks::from_fields(self.cu= rrent)) }; > + // SAFETY: > + // * All values in a list are stored in an `Arc`. > + // * The value cannot be removed from the list for the duration = of the lifetime annotated > + // on the returned `ArcBorrow`, because removing it from the l= ist would require mutable > + // access to the cursor or the list. However, the `ArcBorrow` = holds an immutable borrow > + // on the cursor, which in turn holds an immutable borrow on t= he list, so any such The cursor has a mutable borrow on the list. > + // mutable access requires first releasing the immutable borro= w on the cursor. > + // * Values in a list never have a `UniqueArc` reference. Is there some type invariant guaranteeing this? --=20 Cheers, Benno > + unsafe { ArcBorrow::from_raw(me) } > + } > + > + /// Move the cursor to the next element. > + pub fn next(self) -> Option> { > + // SAFETY: The `current` field is always in a list. > + let next =3D unsafe { (*self.current).next }; > + > + if next =3D=3D self.list.first { > + None > + } else { > + Some(Cursor { > + current: next, > + list: self.list, > + }) > + } > + }