Struct abi_stable::sabi_types::NulStr [−][src]
#[repr(transparent)]pub struct NulStr<'a> { /* fields omitted */ }
Expand description
A utf8 nul-terminated immutable borrowed string.
For the purpose of passing NulStr
s to C,
this has the same ABI as a std::ptr::NonNull<u8>
,
and an Option<NulStr<'_>>
has the same ABI as *const u8
.
Safety
NulStr
has these safety requirement:
- the string must be valid to read for the
'a
lifetime - the string must be utf8 encoded
- the string must be nul terminated
- the string must not be mutated while this is alive
(the same semantics as
&
references)
Example
Passing to extern function
You can pass NulStr
to C functions expecting a nul-terminated string.
use abi_stable::sabi_types::NulStr;
extern "C" {
// the signature in the C side is `uint64_t add_digits(const char*)`
fn add_digits(_: NulStr<'_>) -> u64;
}
const FOO: NulStr<'_> = NulStr::from_str("1.2.3\0");
const BAR: NulStr<'_> = NulStr::from_str("12|34\0");
const QUX: NulStr<'_> = NulStr::from_str("123_abcd_45\0");
assert_eq!(unsafe { add_digits(FOO) }, 6);
assert_eq!(unsafe { add_digits(BAR) }, 10);
assert_eq!(unsafe { add_digits(QUX) }, 15);
Implementations
Constructs an NulStr
from a string slice.
Correctness
If the string contains interior nuls, the first nul will be considered the string terminator.
Panics
This panics when the string does not end with '\0'
.
Example
use abi_stable::sabi_types::NulStr;
const FOO: NulStr<'_> = NulStr::from_str("foo\0");
// `NulStr`s can be compared with `str`s
assert_eq!(FOO, "foo");
const BAR: NulStr<'_> = NulStr::from_str("bar\0");
assert_eq!(BAR, "bar");
const HEWWO: NulStr<'_> = NulStr::from_str("Hello, world!\0");
assert_eq!(HEWWO, "Hello, world!");
const TRUNCATED: NulStr<'_> = NulStr::from_str("baz\0world!\0");
assert_eq!(TRUNCATED, "baz");
Constructs an NulStr from a string slice.
Errors
This returns a NulStrError::NoNulTerminator
when the string does not end
with '\0'
.
This returns a NulStrError::InnerNul
when the string contains a
'\0'
before the '\0'
terminator.
Example
use abi_stable::sabi_types::{NulStr, NulStrError};
// `NulStr`s can be compared with `str`s
assert_eq!(NulStr::try_from_str("hello\0").unwrap(), "hello");
assert_eq!(
NulStr::try_from_str("hello\0world\0"),
Err(NulStrError::InnerNul { pos: 5 }),
);
Constructs an NulStr from a pointer.
Safety
The same as the type-level safety docs
Correctness
If the string contains interior nuls, the first nul will be considered the string terminator.
Example
use abi_stable::sabi_types::NulStr;
const FOO: NulStr<'_> = unsafe { NulStr::from_ptr("foo\0".as_ptr()) };
assert_eq!(FOO, "foo");
const BAR: NulStr<'_> = unsafe { NulStr::from_ptr("bar\0".as_ptr()) };
assert_eq!(BAR, "bar");
const HEWWO: NulStr<'_> = unsafe { NulStr::from_ptr("Hello, world!\0".as_ptr()) };
assert_eq!(HEWWO, "Hello, world!");
const TRUNCATED: NulStr<'_> = unsafe { NulStr::from_ptr("baz\0world!\0".as_ptr()) };
assert_eq!(TRUNCATED, "baz");
Gets a pointer to the start of this nul-terminated string.
Example
use abi_stable::sabi_types::NulStr;
let foo_str = "foo\0";
let foo = NulStr::from_str(foo_str);
assert_eq!(foo.as_ptr(), foo_str.as_ptr());
Converts this NulStr<'a>
to a &'a str
,including the nul byte.
Performance
This conversion requires traversing through the entire string to find the nul byte.
Example
use abi_stable::sabi_types::NulStr;
const FOO: NulStr<'_> = NulStr::from_str("foo bar\0");
let foo: &str = FOO.to_str_with_nul();
assert_eq!(&foo[..3], "foo");
assert_eq!(&foo[4..], "bar\0");
Converts this NulStr<'a>
to a RStr<'a>
,including the nul byte.
Performance
This conversion requires traversing through the entire string to find the nul byte.
Example
use abi_stable::sabi_types::NulStr;
use abi_stable::std_types::RStr;
const BAZ: NulStr<'_> = NulStr::from_str("baz qux\0");
let baz: RStr<'_> = BAZ.to_rstr_with_nul();
assert_eq!(&baz[..3], "baz");
assert_eq!(&baz[4..], "qux\0");
Converts this NulStr<'a>
to a &'a str
,not including the nul byte.
Performance
This conversion requires traversing through the entire string to find the nul byte.
Example
use abi_stable::sabi_types::NulStr;
const FOO: NulStr<'_> = NulStr::from_str("foo bar\0");
let foo: &str = FOO.to_str();
assert_eq!(&foo[..3], "foo");
assert_eq!(&foo[4..], "bar");
Converts this NulStr<'a>
to a RStr<'a>
,not including the nul byte.
Performance
This conversion requires traversing through the entire string to find the nul byte.
Example
use abi_stable::sabi_types::NulStr;
use abi_stable::std_types::RStr;
const BAZ: NulStr<'_> = NulStr::from_str("baz qux\0");
let baz: RStr<'_> = BAZ.to_rstr();
assert_eq!(&baz[..3], "baz");
assert_eq!(&baz[4..], "qux");
Trait Implementations
type StaticEquivalent = _static_NulStr<'static>
This method returns an ordering between self
and other
values if one exists. Read more
This method tests less than (for self
and other
) and is used by the <
operator. Read more
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
type IsNonZeroType = <NonNull<u8> as __StableAbi>::IsNonZeroType
type IsNonZeroType = <NonNull<u8> as __StableAbi>::IsNonZeroType
Whether this type has a single invalid bit-pattern. Read more
The layout of the type provided by implementors.
const
-equivalents of the associated types.
Auto Trait Implementations
impl<'a> RefUnwindSafe for NulStr<'a>
impl<'a> UnwindSafe for NulStr<'a>
Blanket Implementations
Mutably borrows from an owned value. Read more
type ROwned = T
type ROwned = T
The owned type, stored in RCow::Owned
type ForSelf = WithMetadata_<T, T>
type ForSelf = WithMetadata_<T, T>
This is always WithMetadata_<Self, Self>
Compares the address of self
with the address of other
. Read more
Emulates the pipeline operator, allowing method syntax in more places. Read more
The same as piped
except that the function takes &Self
Useful for functions that take &Self
instead of Self
. Read more
The same as piped
, except that the function takes &mut Self
.
Useful for functions that take &mut Self
instead of Self
. Read more
Mutates self using a closure taking self by mutable reference, passing it along the method chain. Read more
Observes the value of self, passing it along unmodified. Useful in long method chains. Read more
Performs a conversion with Into
.
using the turbofish .into_::<_>()
syntax. Read more
Performs a reference to reference conversion with AsRef
,
using the turbofish .as_ref_::<_>()
syntax. Read more
Performs a mutable reference to mutable reference conversion with AsMut
,
using the turbofish .as_mut_::<_>()
syntax. Read more
unsafe fn transmute_element<T>(
self
) -> <Self as CanTransmuteElement<T>>::TransmutedPtr where
Self: CanTransmuteElement<T>,
unsafe fn transmute_element<T>(
self
) -> <Self as CanTransmuteElement<T>>::TransmutedPtr where
Self: CanTransmuteElement<T>,
Transmutes the element type of this pointer.. Read more
type Type = T
type Type = T
This is always Self
.
Converts a value back to the original type.
Converts a mutable reference back to the original type.
alloc
only.Converts a box back to the original type.
Converts a value back to the original type.
Converts a reference back to the original type.
Converts a mutable reference back to the original type.
alloc
only.Converts a box back to the original type.
alloc
only.Converts an Arc back to the original type.
alloc
only.Converts an Rc back to the original type.