Re: [PATCH v8 4/6] rust: debugfs: Support arbitrary owned backing for File

From: Matthew Maurer
Date: Mon Jun 30 2025 - 13:37:04 EST


On Mon, Jun 30, 2025 at 10:34 AM Matthew Maurer <mmaurer@xxxxxxxxxx> wrote:
>
> On Mon, Jun 30, 2025 at 10:30 AM Danilo Krummrich <dakr@xxxxxxxxxx> wrote:
> >
> > On 6/28/25 1:18 AM, Matthew Maurer wrote:
> > > + fn create_file<D: ForeignOwnable>(&self, _name: &CStr, data: D) -> File
> > > + where
> > > + for<'a> D::Borrowed<'a>: Display,
> > > + {
> > > + File {
> > > + _foreign: ForeignHolder::new(data),
> > > + }
> > > }
> >
> > What's the motivation for the ForeignHolder abstraction? Why not just make it
> > File<D> and store data directly?
>
> 1. A `File<D>` can't be held in collection data structures as easily
> unless all your files contain the *same* backing type.
> 2. None of the APIs or potential APIs for `File` care about which type
> it's wrapping, nor are they supposed to. If nothing you can do with a
> `File` is different depending on the backing type, making it
> polymorphic is just needlessly confusing.

If it helps, effectively what I'm doing is adding a `dyn*
ForeignOwnable`, but `dyn*` isn't available, so writing this without
`ForeignHolder` would involve an unnecessary `Box` to hide the `dyn`.