Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 14fbc8d

Browse files
committedMar 14, 2025
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: rust-lang#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 14fbc8d

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-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

‎src/tools/linkchecker/main.rs

+12
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ const LINKCHECK_EXCEPTIONS: &[(&str, &[&str])] = &[
4646
// The docs in std::primitive use proper intra-doc links, so these seem fine to special-case.
4747
// Most these are broken because liballoc uses `#[lang_item]` magic to define things on
4848
// primitives that aren't available in core.
49+
("alloc/ffi/c_str/struct.CString.html", &["#method.sort_by_key"]),
50+
("alloc/ffi/struct.CString.html", &["#method.sort_by_key"]),
4951
("alloc/slice/trait.Join.html", &["#method.join"]),
5052
("alloc/slice/trait.Concat.html", &["#method.concat"]),
5153
("alloc/slice/index.html", &["#method.concat", "#method.join"]),
@@ -73,6 +75,16 @@ const LINKCHECK_EXCEPTIONS: &[(&str, &[&str])] = &[
7375
"core\\bstr\\slice::sort_by_key",
7476
"#method.sort_by_cached_key"
7577
]),
78+
("core/ffi/c_str/struct.CStr.html", &[
79+
"#method.to_ascii_uppercase",
80+
"#method.to_ascii_lowercase",
81+
"core/ffi/c_str/slice::sort_by_key",
82+
]),
83+
("core/ffi/struct.CStr.html", &[
84+
"#method.to_ascii_uppercase",
85+
"#method.to_ascii_lowercase",
86+
"core/ffi/slice::sort_by_key",
87+
]),
7688
("core/primitive.str.html", &["#method.to_ascii_uppercase", "#method.to_ascii_lowercase"]),
7789
("core/primitive.slice.html", &["#method.to_ascii_uppercase", "#method.to_ascii_lowercase",
7890
"core/slice::sort_by_key", "core\\slice::sort_by_key",

‎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)
Failed to load comments.