From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 7925F1AF0B7; Tue, 18 Feb 2025 11:14:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739877275; cv=none; b=UO1EKNTW90FMtWN5TfTKSi4kvC9PJKJo2u/OG94GrtPy4rsWmaB9zgWW++RG3rHlIqlm+Buj0AHRZkeHbg+BKl/4lHhJQ3Jgj+qhA1d0SUXNICj+Ml/PvFYbWjy9RA9Nd6ha7UDvyE2NKkcOAClbxeHuGfR5ivcmiJXTwUkVIb0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739877275; c=relaxed/simple; bh=IJ6yk/QMp2lwd5JD3ubjRFeF4Y21dutz524dIqj2VQE=; h=From:To:Cc:Subject:In-Reply-To:References:Date:Message-ID: MIME-Version:Content-Type; b=emUWag3dAxMUdvD/Ds02h9Hvw8f9xMEksqgTHNpqRYd1y6XmTNUzP3W9b3cPJKT3qd9DGAKnFVn//sofpzqcBGdfBWi8xSz7zGZpgnGkuPu76ZCvPgXQTSXQFP/KlaP4DlfLHUkTdVbHvnrzJLzqSkdfg+JWEsBzsmwCVwR+f9c= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=grWEqFl9; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="grWEqFl9" Received: by smtp.kernel.org (Postfix) with ESMTPSA id DD101C4CEE6; Tue, 18 Feb 2025 11:14:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1739877274; bh=IJ6yk/QMp2lwd5JD3ubjRFeF4Y21dutz524dIqj2VQE=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=grWEqFl98XyZ8ZR+hqyfQRWZ646Tq/KbuB+Hmhi+CTz0FUCKnOrfNogIFyP5F1d0n TroMNyM3n8C3sIrSI3prYmbaNb4tVH0ygXh8S0b0JD2p/KxP7xcfMde7DoJ8pHBRCi duC4B+ovd5dGF1KWfwL8if/sgaujwTqf6kH+b4igLWMpfjiJp3EG0BnC8glDN/v0eV 4B5W71T9y17M97hUFe10mSl2THUrtaMenIENiQ0vUvynEzE8lqh1lletL5EiXJR0mz tnPl6aa2H7mZFYDJa33rTQkKze0etK+i632YUwT9w3bipjV0wUtobf6zLgNADhdz4C Kzi/NwYaT0rIw== From: Andreas Hindborg To: "Gary Guo" Cc: "Miguel Ojeda" , "Alex Gaynor" , "Boqun Feng" , =?utf-8?Q?Bj=C3=B6rn?= Roy Baron , "Benno Lossin" , "Alice Ryhl" , "Masahiro Yamada" , "Nathan Chancellor" , "Nicolas Schier" , "Luis Chamberlain" , "Trevor Gross" , "Adam Bratschi-Kaye" , , , , "Petr Pavlu" , "Sami Tolvanen" , "Daniel Gomez" , "Simona Vetter" , "Greg KH" , Subject: Re: [PATCH v6 2/6] rust: str: implement `Index` for `BStr` In-Reply-To: <20250212090914.6ef7e83b@eugeo> (Gary Guo's message of "Wed, 12 Feb 2025 09:09:14 +0000") References: <20250211-module-params-v3-v6-0-24b297ddc43d@kernel.org> <20250211-module-params-v3-v6-2-24b297ddc43d@kernel.org> <20250211164004.6de768c3@eugeo> <87lduc44c3.fsf@kernel.org> <2m_bB1GvgxV0DoM1PIggnkJD83g9AA6EeW5bgH1JmfZBG1P8D_DE966KAg53gCK8KmWgjpdCUPU6T-5xuZ-FYg==@protonmail.internalid> <20250212090914.6ef7e83b@eugeo> User-Agent: mu4e 1.12.7; emacs 29.4 Date: Tue, 18 Feb 2025 12:14:19 +0100 Message-ID: <87wmdnfqt0.fsf@kernel.org> Precedence: bulk X-Mailing-List: linux-modules@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain "Gary Guo" writes: > On Tue, 11 Feb 2025 21:24:44 +0100 > Andreas Hindborg wrote: > >> "Gary Guo" writes: >> >> > On Tue, 11 Feb 2025 16:57:36 +0100 >> > Andreas Hindborg wrote: >> > >> >> The `Index` implementation on `BStr` was lost when we switched `BStr` from >> >> a type alias of `[u8]` to a newtype. This patch adds back `Index` by >> >> implementing `Index` for `BStr` when `Index` would be implemented for >> >> `[u8]`. >> >> >> >> Signed-off-by: Andreas Hindborg >> >> --- >> >> rust/kernel/str.rs | 11 +++++++++++ >> >> 1 file changed, 11 insertions(+) >> >> >> >> diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs >> >> index 002dcddf7c768..1eb945bed77d6 100644 >> >> --- a/rust/kernel/str.rs >> >> +++ b/rust/kernel/str.rs >> >> @@ -114,6 +114,17 @@ fn eq(&self, other: &Self) -> bool { >> >> } >> >> } >> >> >> >> +impl Index for BStr >> >> +where >> >> + Idx: core::slice::SliceIndex<[u8], Output = [u8]>, >> > >> > I think I'd prefer >> > >> > [T]: Index, >> >> Is that equivalent? > > Sorry, I meant `[u8]: Index`. This makes more semantic sense that > "what ever can index a byte slice, it can also index BStr". This is > also how our CStr and the array primitive type implements its Index > operation. > > They should be equivalent as libcore does > > impl Index for [T] where I: SliceIndex<[T]> { ... } > What I originally wrote is `Idx` must be usable as an index for `[u8]`, yielding `[u8]` when indexing. The new one you suggest, I parse as `[u8]` should be indexable by `Idx`. This is less info. The compiler will also complain about the missing info: error[E0308]: mismatched types --> /home/aeh/src/linux-rust/module-params/rust/kernel/str.rs:141:26 | 141 | BStr::from_bytes(&self.0[index]) | ---------------- ^^^^^^^^^^^^^^ expected `&[u8]`, found `&<[u8] as Index>::Output` | | | arguments to this function are incorrect | = note: expected reference `&[u8]` found reference `&<[u8] as Index>::Output` = help: consider constraining the associated type `<[u8] as Index>::Output` to `[u8]` If I constrain the output it's all fine again: [u8]: Index, But as I said, I don't think it matters which direction we put this? Best regards, Andreas Hindborg