Re: [PATCH v2 1/4] rust: debugfs: Bind DebugFS directory creation

From: Matthew Maurer
Date: Thu May 01 2025 - 12:03:21 EST


On Thu, May 1, 2025 at 3:16 AM Danilo Krummrich <dakr@xxxxxxxxxx> wrote:
>
> On Wed, Apr 30, 2025 at 11:31:56PM +0000, Matthew Maurer wrote:
> >
> > + /// Create a DebugFS subdirectory.
> > + ///
> > + /// # Examples
> > + ///
> > + /// ```
> > + /// # use kernel::c_str;
> > + /// # use kernel::debugfs::Dir;
> > + /// {
> > + /// let parent = Dir::new(c_str!("parent"));
> > + /// // parent exists in DebugFS here.
> > + /// let child = parent.subdir(c_str!("child"));
> > + /// // parent/child exists in DebugFS here.
> > + /// }
> > + /// // Neither exist here.
> > + /// ```
> > + pub fn subdir(&self, name: &CStr) -> Self {
> > + Self::create(name, Some(self))
> > + }
>
> I think this should return a new type (SubDir), which is a transparent wrapper
> of Dir and dereferences to Dir.
>
> Subsequently, we can remove Dir::keep() implement SubDir::keep() instead. This
> ensures that we can never call keep() on the root directory, which would always
> be a bug.
1. If the code in question is builtin rather than a module, discarding
this without tearing it down may not be a bug.
2. Users could always write `core::mem::forget()`, so this will always
be reachable (even if we decide to remove `::keep` to make it harder
to choose).
>
> As an alternative to the Deref impl, you can also implement
> `From<SubDir> for Dir`, such that a SubDir can either be "kept" or converted to
> a Dir. Probably, that's even better.

Yes, this was the "extra type complexity" I referenced in the cover
letter that I was considering doing. I think that probably what I'll
do for v3 is to have both the `Deref` *and* `From` implementation, so
that `SubDir` still automatically gets all of `Dir`s stuff, since your
later `File` comment convinces me we can't just have everything be
`Dir`.