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 2747c7e

Browse files
committedMar 16, 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. Add `AsRef<ByteStr>` as well. 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 2747c7e

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed
 

‎library/core/src/ffi/c_str.rs

+22
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ use crate::{fmt, ops, slice, str};
2525
/// The `CStr` can then be converted to a Rust <code>&[str]</code> by performing
2626
/// UTF-8 validation, or into an owned `CString`.
2727
///
28+
/// The `CStr` [`Deref`] implementation has the same semantics as
29+
/// [`CStr::to_bytes`]; the trailing nul terminator is **omitted** from
30+
/// [`Deref::Target`].
31+
///
2832
/// `&CStr` is to `CString` as <code>&[str]</code> is to `String`: the former
2933
/// in each pair are borrowed references; the latter are owned
3034
/// strings.
@@ -87,6 +91,8 @@ use crate::{fmt, ops, slice, str};
8791
/// ```
8892
///
8993
/// [str]: prim@str "str"
94+
/// [`Deref`]: crate::ops::Deref
95+
/// [`Deref::Target`]: crate::ops::Deref::Target
9096
#[derive(PartialEq, Eq, Hash)]
9197
#[stable(feature = "core_c_str", since = "1.64.0")]
9298
#[rustc_diagnostic_item = "cstr_type"]
@@ -710,6 +716,22 @@ impl AsRef<CStr> for CStr {
710716
}
711717
}
712718

719+
#[unstable(feature = "bstr", issue = "134915")]
720+
impl AsRef<crate::bstr::ByteStr> for CStr {
721+
#[inline]
722+
fn as_ref(&self) -> &crate::bstr::ByteStr {
723+
crate::bstr::ByteStr::from_bytes(self.to_bytes())
724+
}
725+
}
726+
727+
impl ops::Deref for CStr {
728+
type Target = crate::bstr::ByteStr;
729+
730+
fn deref(&self) -> &Self::Target {
731+
self.as_ref()
732+
}
733+
}
734+
713735
/// Calculate the length of a nul-terminated string. Defers to C's `strlen` when possible.
714736
///
715737
/// # 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.