pub struct Strn<'a> {
pub rep: StrnRep<'a>,
}Expand description
Represents a null-terminated string, suitable for passing to C APIs as * const char.
The string can be a null-terminated byte string created in Rust, or a pointer to a null-terminated string returned by C.
Pointer may be null.
Fields§
§rep: StrnRep<'a>Either a byte string terminated with null, or a pointer to a null-terminated string
Implementations§
Source§impl<'a> Strn<'a>
impl<'a> Strn<'a>
Sourcepub fn new(bs: &[u8]) -> Strn<'_>
pub fn new(bs: &[u8]) -> Strn<'_>
Create a new Strn with a byte string. Fail if the last byte is not zero.
Strn::new(b"network\0")
strn!("network")Sourcepub fn from_cstr(cstr: *const u8) -> Strn<'a>
pub fn from_cstr(cstr: *const u8) -> Strn<'a>
Create a new Strn with a null-terminated string pointer returned by C.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Return the length of the string, excluding the terminating null. For safety, we limit to 128.
Sourcepub fn as_cstr(&self) -> *const u8
pub fn as_cstr(&self) -> *const u8
Return the byte string as a null-terminated * const char C-style string.
Fail if the last byte is not zero.
Sourcepub fn as_bytestr(&self) -> &'a [u8]
pub fn as_bytestr(&self) -> &'a [u8]
Return the byte string. Fail if the last byte is not zero.
Sourcepub fn validate_bytestr(bs: &'static [u8])
pub fn validate_bytestr(bs: &'static [u8])
Fail if the last byte is not zero.
Trait Implementations§
impl<'a> Copy for Strn<'a>
impl<'a> Send for Strn<'a>
Allow threads to share Strn, since it is static.
impl<'a> Sync for Strn<'a>
Allow threads to share Strn, since it is static.