Skip to content

Commit 34c1319

Browse files
committed
Implement Deref<Target=ByteStr> for CStr
This produces a deref chain of `CStr` -> `BStr` -> `[u8]` which is present in the Rust-for-Linux analogues of these types. Link: #134915 Link: Rust-for-Linux/linux#1075 Link: https://lore.kernel.org/all/20250221142816.0c015e9f@eugeo/ Link: Rust-for-Linux/linux#1146
1 parent f7b4354 commit 34c1319

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

library/core/src/ffi/c_str.rs

+8
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,14 @@ impl AsRef<CStr> for CStr {
710710
}
711711
}
712712

713+
impl ops::Deref for CStr {
714+
type Target = crate::bstr::ByteStr;
715+
716+
fn deref(&self) -> &Self::Target {
717+
crate::bstr::ByteStr::from_bytes(self.to_bytes())
718+
}
719+
}
720+
713721
/// Calculate the length of a nul-terminated string. Defers to C's `strlen` when possible.
714722
///
715723
/// # Safety

tests/ui/associated-types/associated-types-in-ambiguous-context.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ LL - type X = std::ops::Deref::Target;
4949
LL + type X = <ByteString as Deref>::Target;
5050
|
5151
LL - type X = std::ops::Deref::Target;
52-
LL + type X = <CString as Deref>::Target;
52+
LL + type X = <CStr as Deref>::Target;
5353
|
5454
LL - type X = std::ops::Deref::Target;
55-
LL + type X = <IoSlice<'_> as Deref>::Target;
55+
LL + type X = <CString as Deref>::Target;
5656
|
5757
and N other candidates
5858

0 commit comments

Comments
 (0)