Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Deref<Target=ByteStr> for CStr #138498

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions library/core/src/ffi/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ use crate::{fmt, ops, slice, str};
/// The `CStr` can then be converted to a Rust <code>&[str]</code> by performing
/// UTF-8 validation, or into an owned `CString`.
///
/// The `CStr` [`Deref`] implementation has the same semantics as
/// [`CStr::to_bytes`]; the trailing nul terminator is **omitted** from
/// [`Deref::Target`].
///
/// `&CStr` is to `CString` as <code>&[str]</code> is to `String`: the former
/// in each pair are borrowed references; the latter are owned
/// strings.
Expand Down Expand Up @@ -87,6 +91,8 @@ use crate::{fmt, ops, slice, str};
/// ```
///
/// [str]: prim@str "str"
/// [`Deref`]: crate::ops::Deref
/// [`Deref::Target`]: crate::ops::Deref::Target
#[derive(PartialEq, Eq, Hash)]
#[stable(feature = "core_c_str", since = "1.64.0")]
#[rustc_diagnostic_item = "cstr_type"]
Expand Down Expand Up @@ -710,6 +716,22 @@ impl AsRef<CStr> for CStr {
}
}

#[unstable(feature = "bstr", issue = "134915")]
impl AsRef<crate::bstr::ByteStr> for CStr {
#[inline]
fn as_ref(&self) -> &crate::bstr::ByteStr {
crate::bstr::ByteStr::from_bytes(self.to_bytes())
}
}

impl ops::Deref for CStr {
type Target = crate::bstr::ByteStr;

fn deref(&self) -> &Self::Target {
self.as_ref()
}
}

/// Calculate the length of a nul-terminated string. Defers to C's `strlen` when possible.
///
/// # Safety
Expand Down
12 changes: 12 additions & 0 deletions src/tools/linkchecker/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ const LINKCHECK_EXCEPTIONS: &[(&str, &[&str])] = &[
// The docs in std::primitive use proper intra-doc links, so these seem fine to special-case.
// Most these are broken because liballoc uses `#[lang_item]` magic to define things on
// primitives that aren't available in core.
("alloc/ffi/c_str/struct.CString.html", &["#method.sort_by_key"]),
("alloc/ffi/struct.CString.html", &["#method.sort_by_key"]),
("alloc/slice/trait.Join.html", &["#method.join"]),
("alloc/slice/trait.Concat.html", &["#method.concat"]),
("alloc/slice/index.html", &["#method.concat", "#method.join"]),
Expand Down Expand Up @@ -73,6 +75,16 @@ const LINKCHECK_EXCEPTIONS: &[(&str, &[&str])] = &[
"core\\bstr\\slice::sort_by_key",
"#method.sort_by_cached_key"
]),
("core/ffi/c_str/struct.CStr.html", &[
"#method.to_ascii_uppercase",
"#method.to_ascii_lowercase",
"core/ffi/c_str/slice::sort_by_key",
]),
("core/ffi/struct.CStr.html", &[
"#method.to_ascii_uppercase",
"#method.to_ascii_lowercase",
"core/ffi/slice::sort_by_key",
]),
("core/primitive.str.html", &["#method.to_ascii_uppercase", "#method.to_ascii_lowercase"]),
("core/primitive.slice.html", &["#method.to_ascii_uppercase", "#method.to_ascii_lowercase",
"core/slice::sort_by_key", "core\\slice::sort_by_key",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ LL - type X = std::ops::Deref::Target;
LL + type X = <ByteString as Deref>::Target;
|
LL - type X = std::ops::Deref::Target;
LL + type X = <CString as Deref>::Target;
LL + type X = <CStr as Deref>::Target;
|
LL - type X = std::ops::Deref::Target;
LL + type X = <IoSlice<'_> as Deref>::Target;
LL + type X = <CString as Deref>::Target;
|
and N other candidates

Expand Down
Loading