On Fri, Jan 16, 2026 at 1:50 PM Timur Tabi <[email protected]> wrote:
>
> Add Dir::empty() which returns a &'static reference to a no-op Dir.
> All file and subdirectory operations on this directory silently succeed
> without creating actual filesystem entries.
I thought the whole point of Greg's admonition to never report errors
and to automatically create no-op structures if debugfs was disable
was that we *don't* do things like this, and instead you just do
something like
```
// Always do this, whether or not debugfs is available.
let debugfs_root = Dir::new("my_thing");
debugfs_root.scope(data, name, |d, s| { ... })
```
If DebugFS is not available, it already returns an empty fake
directory. That variant of a fake directory is also size 0, so you
aren't paying anything to use it...
>
> This enables callers to use a single code path regardless of whether
> debugfs is available at runtime. For example:
>
> let dir = optional_debugfs_root.unwrap_or_else(|| Dir::empty());
> dir.scope(data, name, |d, s| { ... })
>
> The returned reference has 'static lifetime, allowing initializers
> returned by scope() to be used outside the immediate scope where the
> Dir reference was obtained.
>
> Signed-off-by: Timur Tabi <[email protected]>
> ---
> rust/kernel/debugfs.rs | 18 ++++++++++++++++++
> 1 file changed, 18 insertions(+)
>
> diff --git a/rust/kernel/debugfs.rs b/rust/kernel/debugfs.rs
> index facad81e8290..44ff2b37786a 100644
> --- a/rust/kernel/debugfs.rs
> +++ b/rust/kernel/debugfs.rs
> @@ -110,6 +110,24 @@ pub fn new(name: &CStr) -> Self {
> Dir::create(name, None)
> }
>
> + /// Returns a reference to a static no-op directory that doesn't create
> any debugfs entries.
> + ///
> + /// All file and subdirectory creation operations on this directory will
> silently succeed
> + /// without creating actual filesystem entries. This is useful when you
> want to conditionally
> + /// enable debugfs but use the same code path regardless.
> + pub fn empty() -> &'static Self {
> + #[cfg(CONFIG_DEBUG_FS)]
> + {
> + static EMPTY: Dir = Dir(None);
> + &EMPTY
> + }
> + #[cfg(not(CONFIG_DEBUG_FS))]
> + {
> + static EMPTY: Dir = Dir();
> + &EMPTY
> + }
> + }
> +
> /// Creates a subdirectory within this directory.
> ///
> /// # Examples
> --
> 2.52.0
>