[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <DD92QHU14T9S.14AE7Z1UWJ6ZT@kernel.org>
Date: Sat, 04 Oct 2025 01:26:07 +0200
From: "Danilo Krummrich" <dakr@...nel.org>
To: "Matthew Maurer" <mmaurer@...gle.com>
Cc: <gregkh@...uxfoundation.org>, <rafael@...nel.org>, <ojeda@...nel.org>,
<alex.gaynor@...il.com>, <boqun.feng@...il.com>, <gary@...yguo.net>,
<bjorn3_gh@...tonmail.com>, <lossin@...nel.org>, <a.hindborg@...nel.org>,
<aliceryhl@...gle.com>, <tmgross@...ch.edu>,
<rust-for-linux@...r.kernel.org>, <linux-kernel@...r.kernel.org>
Subject: Re: [PATCH 4/7] rust: debugfs: support blobs from smart pointers
On Sat Oct 4, 2025 at 1:12 AM CEST, Matthew Maurer wrote:
> On Fri, Oct 3, 2025 at 3:27 PM Danilo Krummrich <dakr@...nel.org> wrote:
>> +// Base implementation for any `T: AsBytes`.
>> impl<T: AsBytes> BinaryWriter for T {
>> fn write_to_slice(&self, writer: &mut UserSliceWriter, offset: usize) -> Result<usize> {
>> writer.write_slice_partial(self.as_bytes(), offset)
>> }
>> }
>>
>> +// Delegate for `Mutex<T>`: Support a `T` with an outer mutex.
>> impl<T: BinaryWriter> BinaryWriter for Mutex<T> {
>> fn write_to_slice(&self, writer: &mut UserSliceWriter, offset: usize) -> Result<usize> {
>> let guard = self.lock();
>> @@ -64,6 +69,56 @@ fn write_to_slice(&self, writer: &mut UserSliceWriter, offset: usize) -> Result<
>> }
>> }
>>
>> +// Delegate for `Box<T, A>`: Support a `Box<T, A>` with no lock or an inner lock.
>> +impl<T, A> BinaryWriter for Box<T, A>
>> +where
>> + T: BinaryWriter,
>> + A: Allocator,
>> +{
>> + fn write_to_slice(&self, writer: &mut UserSliceWriter, offset: usize) -> Result<usize> {
>> + self.deref().write_to_slice(writer, offset)
>> + }
>> +}
>> +
>> +// Delegate for `Pin<Box<T, A>>`: Support a `Pin<Box<T, A>>` with no lock or an inner lock.
>> +impl<T, A> BinaryWriter for Pin<Box<T, A>>
>> +where
>> + T: BinaryWriter,
>> + A: Allocator,
>> +{
>> + fn write_to_slice(&self, writer: &mut UserSliceWriter, offset: usize) -> Result<usize> {
>> + self.deref().write_to_slice(writer, offset)
>> + }
>> +}
>> +
>> +// Delegate for `Arc<T>`: Support a `Arc<T>` with no lock or an inner lock.
>> +impl<T> BinaryWriter for Arc<T>
>> +where
>> + T: BinaryWriter,
>> +{
>> + fn write_to_slice(&self, writer: &mut UserSliceWriter, offset: usize) -> Result<usize> {
>> + self.deref().write_to_slice(writer, offset)
>> + }
>> +}
>
> For `Box`, `Pin<Box<`, and `Arc`, where the only operation being
> performed is to deref, is there a reason that we couldn't have the
> `File` object be *inside* the object, thus avoiding any need for
> these? I can't see them causing trouble, but
>
> ```
> Box<File<T>>
> Pin<Box<File<T>>>
> Arc<File<T>>
> ```
>
> seem like they'd usually be fine. The one caveat I can think of is
> that if you had other functions that wanted to take an `&Arc<T>` for
> operations on the Arc, then having an `Arc<File<T>>` would be
> suboptimal. Am I missing something?
I think this way around is not compatible with scoped API.
Besides that, there is a semantical difference between File<Arc<T>> and
Arc<File<T>>, i.e. the file itself would become reference counted.
> Depending on the use case I'm missing, would a blanket implementation
> for `T: Deref` in this case and `DerefMut` later on make more sense?
> That should contract these into a single definition and generalize to
> e.g. `ListArc` without further code.
It was also my first thought to generalize over T: Deref, but unfortunately this
does lead to conflicting implementations of BinaryWriter.
>> +// Delegate for `Vec<T, A>`: Support a `Vec<T, A>` with an outer lock.
>> +impl<T, A> BinaryReaderMut for Vec<T, A>
>> +where
>> + T: AsBytes + FromBytes,
>> + A: Allocator,
>> +{
>> + fn read_from_slice_mut(
>> + &mut self,
>> + reader: &mut UserSliceReader,
>> + offset: usize,
>> + ) -> Result<usize> {
>> + let slice = self.as_mut_slice();
>> +
>
> Nit: This is safe, but it also requires `FromBytes`, and is &mut, &mut
It already requires T: AsBytes + FromBytes above. Or do you mean something else?
Powered by blists - more mailing lists