Struct const_cstr::ConstCStr
source · pub struct ConstCStr {
pub val: &'static str,
}
Expand description
A type representing a static C-compatible string, wrapping &'static str
.
Note
Prefer the const_cstr!
macro to create an instance of this struct
over manual initialization. The macro will include the NUL byte for you.
Fields§
§val: &'static str
The wrapped string value. Not intended to be used for manual initialization.
Public only to allow initialization by the const_cstr!
macro.
Includes the NUL terminating byte. Use to_str()
to get an &'static str
without the NUL terminating byte.
Implementations§
source§impl ConstCStr
impl ConstCStr
sourcepub fn to_str(&self) -> &'static str
pub fn to_str(&self) -> &'static str
Returns the wrapped string, without the NUL terminating byte.
Compare to CStr::to_str()
which checks that the string is valid UTF-8 first,
since it starts from an arbitrary pointer instead of a Rust string slice.
sourcepub fn to_bytes(&self) -> &'static [u8] ⓘ
pub fn to_bytes(&self) -> &'static [u8] ⓘ
Returns the wrapped string as a byte slice, without the NUL terminating byte.
sourcepub fn to_bytes_with_nul(&self) -> &'static [u8] ⓘ
pub fn to_bytes_with_nul(&self) -> &'static [u8] ⓘ
Returns the wrapped string as a byte slice, with* the NUL terminating byte.
sourcepub fn as_ptr(&self) -> *const c_char
pub fn as_ptr(&self) -> *const c_char
Returns a pointer to the beginning of the wrapped string.
Suitable for passing to any function that expects a C-compatible string.
Since the underlying string is guaranteed to be 'static
,
the pointer should always be valid.
Panics
If the wrapped string is not NUL-terminated.
(Unlikely if you used the const_cstr!
macro. This is just a sanity check.)
sourcepub fn as_cstr(&self) -> &'static CStr
pub fn as_cstr(&self) -> &'static CStr
Returns the wrapped string as an &'static CStr
, skipping the length check that
CStr::from_ptr()
performs (since we know the length already).
Panics
If the wrapped string is not NUL-terminated.
(Unlikely if you used the const_cstr!
macro. This is just a sanity check.)
Trait Implementations§
source§impl Ord for ConstCStr
impl Ord for ConstCStr
source§impl PartialEq<ConstCStr> for ConstCStr
impl PartialEq<ConstCStr> for ConstCStr
source§impl PartialOrd<ConstCStr> for ConstCStr
impl PartialOrd<ConstCStr> for ConstCStr
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more