Received: by 2002:ab2:2994:0:b0:1ef:ca3e:3cd5 with SMTP id n20csp219595lqb; Thu, 14 Mar 2024 09:24:55 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVwR+84XrnwRg84AES9RlM5+CdDv7hq2VIqJ+UHCnq1+Loe4+QKFZe54sjyDFpSxIBf5ZjgoIfDkUC/SZBbQIP4Or3BDzROnnGpXpyvjQ== X-Google-Smtp-Source: AGHT+IEGQzpAd7E6p3iODQEAHoY5CDKWbt7XeGFHdP6LaX1w/CjXncNk2t/pGEJQbuXmWyLUOnjQ X-Received: by 2002:a17:90b:148d:b0:29c:76a9:3b7b with SMTP id js13-20020a17090b148d00b0029c76a93b7bmr491887pjb.7.1710433495355; Thu, 14 Mar 2024 09:24:55 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710433495; cv=pass; d=google.com; s=arc-20160816; b=b+iI1DUmYTYehmf80ZQkV7kt9LjdfJGqyM6okVSMwBZ9Hkf+NGX+CYNK2OfLgcnOR3 wrBkGRQQ1dIQZRE3PRG/2+8jfLSg/4//7klNtF3z8Pd6Y2VYzGOrjTN61v8Hw5xlYZxC ydh4pyC/dfOh793xUx5uFm4ew6i4jQFotwxF6opuG8TlImjVqGDrMLMY0fOcudBN0hKN Ra+8639EpaFjKGA79+GuhKrSJsy/Y+PffMfLIsJPzdIkDXKzTjyPEi/VNNECfJmJeS6e GS1gB11nmkH48DqjnQKng6wKoYKFe5cfxjgJBoyDCMSoCoBcRCyTqmXky1MX1QNlwJ85 B+uw== 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=Jd4nfT5gXP3CYGNG45QfWm+S59cKf0dh995GCAX2/EY=; fh=jz9ZYdkZU3gmm8weL7yql0hVHDdlmm49R0ERliAl9tY=; b=JF/qnxXyIIPuKZGa36NJ8Y0r44/kVnnvZxls2/7QdiCtW0wEA6aidVtlS3FNH4LdkV pJU9UGtR6d6cWqUCJwbYdhUX6ca1wD9B8n3zM9yYt6d4HV3bUpd7ssjrsMUtcfWzwcQL gbJA+EHstyGB9Buc9dff8DXeGWfLIjQbRLINqF5PpHGIEVRa1V1OLmf0i5x3hsKfX0Oa +DAPFb1IhwVqyyxsV9eXmVm51Rx1gmJyvyfkEhaKTtqr4Tha3NcJaW3SNyUqv0r0tHyR quphgfAY1HXS/3I8hAzUdpX7IFkHk8IGlvOCt33QdzZrUs2yLTiYu8TD5nozrRUyhQB6 xPwQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=glEsPaJa; 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-103566-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-103566-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 h24-20020a17090acf1800b002993b6846d5si928968pju.85.2024.03.14.09.24.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Mar 2024 09:24:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-103566-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=glEsPaJa; 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-103566-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-103566-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 90653B220E5 for ; Thu, 14 Mar 2024 16:24:51 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C7F8473529; Thu, 14 Mar 2024 16:24:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b="glEsPaJa" Received: from mail-40131.protonmail.ch (mail-40131.protonmail.ch [185.70.40.131]) (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 A4FEB5D74C for ; Thu, 14 Mar 2024 16:24:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.70.40.131 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710433482; cv=none; b=WWLZQkJC/XSb5A+u0GfaeKSNohEXjJgJiJ5ePCz8F23p1XLitOmXfipGbzB5eXSA6K5MDNZFlP880UD7B00fr+WWdyJwg7RIc4Kk+uWta5TOaazJcEQRwmR6kaRVBKwk5gsXpJC0o/pXQIY2JBmmNfPNQOODDxVlFu/mEVFKcIE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710433482; c=relaxed/simple; bh=paQCo9lpiP6kOLc1/HV9mO/b3xfvpnDtdbn/vBXj3hs=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=iWmHkhXQSXVwEYUkkdGqjoatjlw3U4A2vcgVy/f3fcj1BBR9UaOfn7VcBnJ3RUoGbF2rsD/D73CFh6ZFaL0/u5RGp2T5mCzveWKltK0WlZdts4piLCNu0SY2rGKG1uLt8VjOJ6oosybjtiAOwpt7s8EmmBTpp4rqu3tGBHvMkRY= 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=glEsPaJa; arc=none smtp.client-ip=185.70.40.131 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=1710433476; x=1710692676; bh=Jd4nfT5gXP3CYGNG45QfWm+S59cKf0dh995GCAX2/EY=; 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=glEsPaJaRFumXfpPsaE8DeR1JXfODs1SHNGgMQN3rcvU0z4EauyHgYsvrQSL/yQxp uOCDytDgmrlbaE8vJdYthT8/hV20PBpF6vOeHwbjbFSNH/N+t4BlINBY/386QOM10p D1SzJvCR9c2D0ejDzMWm4c6SK6htO4XuAcvP4217zF2VCFIKpj6l0sTuY+hUqSEo/m J+2z2uZTS+VD0Zj6wgsOCJUV9j60P6aTTHFGZ/jukK1r2OxmDIZ5glStvPzIGvTVYL kFtHJDwSg9TStNmeQ0usGfWXtP3P9c9RXZ7OMui8e26rXE+x33aTyq60fKRnSmkGF/ GIn5BYu0dGSPA== Date: Thu, 14 Mar 2024 16:24:23 +0000 To: Matt Gilbride , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , Andreas Hindborg , Alice Ryhl , Greg Kroah-Hartman , =?utf-8?Q?Arve_Hj=C3=B8nnev=C3=A5g?= , Todd Kjos , Martijn Coenen , Joel Fernandes , Carlos Llamas , Suren Baghdasaryan , Christian Brauner From: Benno Lossin Cc: Rob Landley , Davidlohr Bueso , Michel Lespinasse , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2 3/6] rust: rbtree: add `RBTreeIterator` Message-ID: <00178dbe-6dfc-48f5-9712-b0b7361d14e4@proton.me> In-Reply-To: <20240219-b4-rbtree-v2-3-0b113aab330d@google.com> References: <20240219-b4-rbtree-v2-0-0b113aab330d@google.com> <20240219-b4-rbtree-v2-3-0b113aab330d@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 2/19/24 12:48, Matt Gilbride wrote: > @@ -350,6 +393,52 @@ fn drop(&mut self) { > } > } >=20 > +impl<'a, K, V> IntoIterator for &'a RBTree { > + type Item =3D (&'a K, &'a V); > + type IntoIter =3D RBTreeIterator<'a, K, V>; > + > + fn into_iter(self) -> Self::IntoIter { > + self.iter() > + } > +} > + > +/// An iterator over the nodes of a [`RBTree`]. > +/// > +/// Instances are created by calling [`RBTree::iter`]. There probably should be some invariants here, like - `self.next` is a valid pointer - `self.next` points to a node stored inside of a valid `RBtree` > +pub struct RBTreeIterator<'a, K, V> { > + _tree: PhantomData<&'a RBTree>, > + next: *mut bindings::rb_node, > +} > + > +// SAFETY: An [`RBTree`] allows the same kinds of access to its values t= hat a struct allows to its > +// fields, so we use the same Send condition as would be used for a stru= ct with K and V fields. > +unsafe impl<'a, K: Send, V: Send> Send for RBTreeIterator<'a, K, V> {} > + > +// SAFETY: An [`RBTree`] allows the same kinds of access to its values t= hat a struct allows to its > +// fields, so we use the same Sync condition as would be used for a stru= ct with K and V fields. > +unsafe impl<'a, K: Sync, V: Sync> Sync for RBTreeIterator<'a, K, V> {} These comments should also be updated. > + > +impl<'a, K, V> Iterator for RBTreeIterator<'a, K, V> { > + type Item =3D (&'a K, &'a V); > + > + fn next(&mut self) -> Option { > + if self.next.is_null() { > + return None; > + } > + > + // SAFETY: All links fields we create are in a `Node`. See my suggestion on patch 2. > + let cur =3D unsafe { crate::container_of!(self.next, Node,= links) }; > + > + // SAFETY: The reference to the tree used to create the iterator= outlives the iterator, so > + // the tree cannot change. By the tree invariant, all nodes are = valid. Why does the pointer come from a tree to begin with? (ie you need the invariants I stated above) --=20 Cheers, Benno > + self.next =3D unsafe { bindings::rb_next(self.next) }; > + > + // SAFETY: By the same reasoning above, it is safe to dereferenc= e the node. Additionally, > + // it is ok to return a reference to members because the iterato= r must outlive it. > + Some(unsafe { (&(*cur).key, &(*cur).value) }) > + } > +} > + > /// A memory reservation for a red-black tree node. > /// > /// It contains the memory needed to hold a node that can be inserted i= nto a red-black tree. One >=20 > -- > 2.44.0.rc0.258.g7320e95886-goog >=20