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.
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