Message ID | 20250218-module-params-v3-v7-4-5e1afabcac1b@kernel.org (mailing list archive) |
---|---|
State | New |
Headers | show |
Series | rust: extend `module!` macro with integer parameter support | expand |
Context | Check | Description |
---|---|---|
mcgrof/vmtest-modules-next-PR | fail | PR summary |
mcgrof/vmtest-modules-next-VM_Test-4 | fail | Logs for setup / Setup kdevops environment |
mcgrof/vmtest-modules-next-VM_Test-1 | success | Logs for Run CI tests |
mcgrof/vmtest-modules-next-VM_Test-2 | fail | Logs for cleanup / Archive results and cleanup |
mcgrof/vmtest-modules-next-VM_Test-3 | fail | Logs for cleanup / Archive results and cleanup |
mcgrof/vmtest-modules-next-VM_Test-0 | success | Logs for Run CI tests |
mcgrof/vmtest-modules-next-VM_Test-5 | fail | Logs for setup / Setup kdevops environment |
Hi Andreas, > On 18 Feb 2025, at 10:00, Andreas Hindborg <a.hindborg@kernel.org> wrote: > > Implement `strip_prefix` for `BStr` by deferring to `slice::strip_prefix` > on the underlying `&[u8]`. > > Reviewed-by: Gary Guo <gary@garyguo.net> > Reviewed-by: Alice Ryhl <aliceryhl@google.com> > Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org> > --- > > It is also possible to get this method by implementing > `core::slice::SlicePattern` for `BStr`. `SlicePattern` is unstable, so this > seems more reasonable. > --- > rust/kernel/str.rs | 16 ++++++++++++++++ > 1 file changed, 16 insertions(+) > > diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs > index c6bd2c69543dc..db272d2198fcc 100644 > --- a/rust/kernel/str.rs > +++ b/rust/kernel/str.rs > @@ -31,6 +31,22 @@ pub const fn from_bytes(bytes: &[u8]) -> &Self { > // SAFETY: `BStr` is transparent to `[u8]`. > unsafe { &*(bytes as *const [u8] as *const BStr) } > } > + > + /// Strip a prefix from `self`. Delegates to [`slice::strip_prefix`]. > + /// > + /// # Example > + /// ``` > + /// use kernel::b_str; > + /// assert_eq!(Some(b_str!("bar")), b_str!("foobar").strip_prefix(b_str!("foo"))); > + /// assert_eq!(None, b_str!("foobar").strip_prefix(b_str!("bar"))); > + /// assert_eq!(Some(b_str!("foobar")), b_str!("foobar").strip_prefix(b_str!(""))); > + /// assert_eq!(Some(b_str!("")), b_str!("foobar").strip_prefix(b_str!("foobar"))); > + /// ``` This is passing. > + pub fn strip_prefix(&self, pattern: impl AsRef<Self>) -> Option<&BStr> { > + self.deref() > + .strip_prefix(pattern.as_ref().deref()) > + .map(Self::from_bytes) > + } > } > > impl fmt::Display for BStr { > > -- > 2.47.0 > > Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com>
diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index c6bd2c69543dc..db272d2198fcc 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -31,6 +31,22 @@ pub const fn from_bytes(bytes: &[u8]) -> &Self { // SAFETY: `BStr` is transparent to `[u8]`. unsafe { &*(bytes as *const [u8] as *const BStr) } } + + /// Strip a prefix from `self`. Delegates to [`slice::strip_prefix`]. + /// + /// # Example + /// ``` + /// use kernel::b_str; + /// assert_eq!(Some(b_str!("bar")), b_str!("foobar").strip_prefix(b_str!("foo"))); + /// assert_eq!(None, b_str!("foobar").strip_prefix(b_str!("bar"))); + /// assert_eq!(Some(b_str!("foobar")), b_str!("foobar").strip_prefix(b_str!(""))); + /// assert_eq!(Some(b_str!("")), b_str!("foobar").strip_prefix(b_str!("foobar"))); + /// ``` + pub fn strip_prefix(&self, pattern: impl AsRef<Self>) -> Option<&BStr> { + self.deref() + .strip_prefix(pattern.as_ref().deref()) + .map(Self::from_bytes) + } } impl fmt::Display for BStr {