Re: [PATCH v2 5/8] rust: debugfs: support blobs from smart pointers

Danilo Krummrich
2025-10-22T09:34:28Z

On 10/22/25 7:57 AM, Alexandre Courbot wrote:
> On Tue Oct 21, 2025 at 7:26 AM JST, Danilo Krummrich wrote:
> <snip>
>> @@ -51,12 +54,14 @@ pub trait BinaryWriter {
>> fn write_to_slice(&self, writer: &mut UserSliceWriter, offset: file::Offset) -> Result<usize>;
>> }
>>
>> +// Base implementation for any `T: AsBytes`.
>> impl<T: AsBytes> BinaryWriter for T {
>> fn write_to_slice(&self, writer: &mut UserSliceWriter, offset: file::Offset) -> Result<usize> {
>> writer.write_slice_partial(self.as_bytes(), offset)
>> }
>> }
>>
>> +// Delegate for `Mutex<T>`: Support a `T` with an outer mutex.
>
> I guess these two comments belong in the previous patch?

I added them in this patch since it is where the comments become useful, but
technically they can indeed go in the previous one.

>> impl<T: BinaryWriter> BinaryWriter for Mutex<T> {
>> fn write_to_slice(&self, writer: &mut UserSliceWriter, offset: file::Offset) -> Result<usize> {
>> let guard = self.lock();
>> @@ -65,6 +70,56 @@ fn write_to_slice(&self, writer: &mut UserSliceWriter, offset: file::Offset) ->
>> }
>> }
>>
>> +// 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: file::Offset) -> 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: file::Offset) -> 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: file::Offset) -> Result<usize> {
>> + self.deref().write_to_slice(writer, offset)
>> + }
>> +}
>
> These 3 implementations are identical - can we replace some/all with
> just an implementation for anything implementing `Deref<T>`?

Unfortunately, this would lead to some ambiguity for the compiler. A type could
match

impl<T: AsBytes> BinaryWriter for T {}

and

impl<T, D> BinaryWriter for T
where
T: Deref<Target = D>,
D: BinaryWriter,
{}

at the same time.