From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-40133.protonmail.ch (mail-40133.protonmail.ch [185.70.40.133]) (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 AF1D0189BBD for ; Tue, 20 Aug 2024 08:08:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.70.40.133 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724141312; cv=none; b=L1X4CUuOdB2RTiHCzpJ91f2SPj75Mbo/+dQz/vgKpwQs3XBi7y0jXefihEEfP+d/fa8znhJ+CVdeyeR2DB2dxsA1TNF3H7I2E6INmn60cZQISdpJCdWt3+xnIqY4dyAp+NTCerc3LP0vfzeE0AGJVkvsuRR9ckbGbU48Licp2/w= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724141312; c=relaxed/simple; bh=pd90YgBG4tBg17cPOjEFD3caGTY+9mJ92uswnxk6IkE=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=KCLHKK/ICr1SZripxy9eXWS8nmdoeUfj0HtZq8tTwO7gayDsNnBEGgjHUqVafJC2nifZBbIChCr4RR0fnHCB+LonxXmY6mb+oJ7MIepLjZ9gmkam1KctUt1vWbBluH+MVXog1jOvJecNIxljhNCbYkt89x+UpSuMj852hD7mJEE= 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=ETXQDx+F; arc=none smtp.client-ip=185.70.40.133 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b="ETXQDx+F" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1724141306; x=1724400506; bh=53+5x/xmXek+DbIG4yjMFIEkhMF4WzX2KOCqZDEYdaM=; 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=ETXQDx+FpztwH++Xyy1bxmN7dL8AFYd03w0HFzh1Py+dioTXjnSmxku+so0A2rF1b Kim+QH0zXvcdbC+8evJD7Kre8ZRDk3uHnzmVp8b/1/UcR4t9J2THbco7ZO5nfR83vo dPtV1Q7mfLs6a9Bw5ojvbBtfrjMXe5PnmNof59oSstTpS+F1McadQmTHufj1/3bVAc XwV1xhVY/v8B+ecqCritye3o7rhBnqfrmtn3FutpH8X3ziPuf1PzK/pT/qGR5iyRJ6 yq64fVB4cIKgGtuYWPivqrwNhtbdW3w2oqR85QnwMlb+R7BtnyclX2/fBeixwUggXl sE/P8YRwPGSGA== Date: Tue, 20 Aug 2024 08:08:22 +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 v10 5/5] rust: rbtree: add `RBTree::entry` Message-ID: <57aa157d-ee94-419d-bad3-0bb93f5c2095@proton.me> In-Reply-To: <20240819-b4-rbtree-v10-5-3b3b2c4d73af@google.com> References: <20240819-b4-rbtree-v10-0-3b3b2c4d73af@google.com> <20240819-b4-rbtree-v10-5-3b3b2c4d73af@google.com> Feedback-ID: 71780778:user:proton X-Pm-Message-ID: 5ab9c56df45f04e8737dfb27924e85c535452cfa Precedence: bulk X-Mailing-List: rust-for-linux@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 19.08.24 17:07, Matt Gilbride wrote: > From: Alice Ryhl >=20 > This mirrors the entry API [1] from the Rust standard library on > `RBTree`. This API can be used to access the entry at a specific key and > make modifications depending on whether the key is vacant or occupied. > This API is useful because it can often be used to avoid traversing the > tree multiple times. >=20 > This is used by binder to look up and conditionally access or insert a > value, depending on whether it is there or not [2]. >=20 > Link: https://doc.rust-lang.org/stable/std/collections/btree_map/enum.Ent= ry.html [1] > Link: https://android-review.googlesource.com/c/kernel/common/+/2849906 [= 2] > Signed-off-by: Alice Ryhl > Tested-by: Alice Ryhl > Reviewed-by: Boqun Feng > Signed-off-by: Matt Gilbride One nit below, but feel free to add if it doesn't work: Reviewed-by: Benno Lossin > --- > rust/kernel/rbtree.rs | 310 ++++++++++++++++++++++++++++++++++++++------= ------ > 1 file changed, 235 insertions(+), 75 deletions(-) [...] > + /// Gets a mutable reference to the value in the entry. > + pub fn get_mut(&mut self) -> &mut V { > + // SAFETY: > + // - `self.node_links` is a valid pointer to a node in the tree. > + // - We have exclusive access to the underlying tree, and can th= us give out a mutable reference. > + unsafe { > + &mut (*(container_of!(self.node_links, Node, links) as= *mut Node)).value Does `.cast_mut()` work here instead of `as *mut Node`? (also below) --- Cheers, Benno > + } > + } > + > + /// Converts the entry into a mutable reference to its value. > + /// > + /// If you need multiple references to the `OccupiedEntry`, see [`se= lf#get_mut`]. > + pub fn into_mut(self) -> &'a mut V { > + // SAFETY: > + // - `self.node_links` is a valid pointer to a node in the tree. > + // - This consumes the `&'a mut RBTree`, therefore it can = give out a mutable reference that lives for `'a`. > + unsafe { > + &mut (*(container_of!(self.node_links, Node, links) as= *mut Node)).value > + } > + }