pub struct NulStr<'a> { /* private fields */ }Expand description
A utf8 nul-terminated immutable borrowed string.
For the purpose of passing NulStrs 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
'alifetime - 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§
Source§impl<'a> NulStr<'a>
impl<'a> NulStr<'a>
Sourcepub const fn from_str(str: &'a str) -> Self
pub const fn from_str(str: &'a str) -> Self
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");
Sourcepub const fn try_from_str(string: &'a str) -> Result<Self, NulStrError>
pub const fn try_from_str(string: &'a str) -> Result<Self, NulStrError>
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 }),
);
Sourcepub const unsafe fn from_ptr(ptr: *const u8) -> Self
pub const unsafe fn from_ptr(ptr: *const u8) -> Self
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");
Sourcepub const fn as_ptr(self) -> *const u8
pub const fn as_ptr(self) -> *const u8
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());
Sourcepub fn to_str_with_nul(&self) -> &'a str
pub fn to_str_with_nul(&self) -> &'a str
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");
Sourcepub fn to_rstr_with_nul(&self) -> RStr<'a>
pub fn to_rstr_with_nul(&self) -> RStr<'a>
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");
Sourcepub fn to_str(self) -> &'a str
pub fn to_str(self) -> &'a str
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");
Sourcepub fn to_rstr(self) -> RStr<'a>
pub fn to_rstr(self) -> RStr<'a>
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§
Source§impl<'a> GetStaticEquivalent_ for NulStr<'a>
impl<'a> GetStaticEquivalent_ for NulStr<'a>
Source§type StaticEquivalent = _static_NulStr<'static>
type StaticEquivalent = _static_NulStr<'static>
'static equivalent of SelfSource§impl<'a> Ord for NulStr<'a>
impl<'a> Ord for NulStr<'a>
Source§impl<'a> PartialOrd for NulStr<'a>
impl<'a> PartialOrd for NulStr<'a>
Source§impl<'a> StableAbi for NulStr<'a>
impl<'a> StableAbi for NulStr<'a>
Source§const LAYOUT: &'static TypeLayout
const LAYOUT: &'static TypeLayout
Source§type IsNonZeroType = <NonNull<u8> as StableAbi>::IsNonZeroType
type IsNonZeroType = <NonNull<u8> as StableAbi>::IsNonZeroType
Source§const ABI_CONSTS: AbiConsts = _
const ABI_CONSTS: AbiConsts = _
const-equivalents of the associated types.impl<'a> Copy for NulStr<'a>
impl<'a> Eq for NulStr<'a>
impl Send for NulStr<'_>
impl Sync for NulStr<'_>
Auto Trait Implementations§
impl<'a> Freeze for NulStr<'a>
impl<'a> RefUnwindSafe for NulStr<'a>
impl<'a> Unpin for NulStr<'a>
impl<'a> UnwindSafe for NulStr<'a>
Blanket Implementations§
Source§impl<T> AlignerFor<1> for T
impl<T> AlignerFor<1> for T
Source§impl<T> AlignerFor<1024> for T
impl<T> AlignerFor<1024> for T
Source§type Aligner = AlignTo1024<T>
type Aligner = AlignTo1024<T>
AlignTo* type which aligns Self to ALIGNMENT.Source§impl<T> AlignerFor<128> for T
impl<T> AlignerFor<128> for T
Source§type Aligner = AlignTo128<T>
type Aligner = AlignTo128<T>
AlignTo* type which aligns Self to ALIGNMENT.Source§impl<T> AlignerFor<16> for T
impl<T> AlignerFor<16> for T
Source§impl<T> AlignerFor<16384> for T
impl<T> AlignerFor<16384> for T
Source§type Aligner = AlignTo16384<T>
type Aligner = AlignTo16384<T>
AlignTo* type which aligns Self to ALIGNMENT.Source§impl<T> AlignerFor<2> for T
impl<T> AlignerFor<2> for T
Source§impl<T> AlignerFor<2048> for T
impl<T> AlignerFor<2048> for T
Source§type Aligner = AlignTo2048<T>
type Aligner = AlignTo2048<T>
AlignTo* type which aligns Self to ALIGNMENT.Source§impl<T> AlignerFor<256> for T
impl<T> AlignerFor<256> for T
Source§type Aligner = AlignTo256<T>
type Aligner = AlignTo256<T>
AlignTo* type which aligns Self to ALIGNMENT.Source§impl<T> AlignerFor<32> for T
impl<T> AlignerFor<32> for T
Source§impl<T> AlignerFor<32768> for T
impl<T> AlignerFor<32768> for T
Source§type Aligner = AlignTo32768<T>
type Aligner = AlignTo32768<T>
AlignTo* type which aligns Self to ALIGNMENT.Source§impl<T> AlignerFor<4> for T
impl<T> AlignerFor<4> for T
Source§impl<T> AlignerFor<4096> for T
impl<T> AlignerFor<4096> for T
Source§type Aligner = AlignTo4096<T>
type Aligner = AlignTo4096<T>
AlignTo* type which aligns Self to ALIGNMENT.Source§impl<T> AlignerFor<512> for T
impl<T> AlignerFor<512> for T
Source§type Aligner = AlignTo512<T>
type Aligner = AlignTo512<T>
AlignTo* type which aligns Self to ALIGNMENT.Source§impl<T> AlignerFor<64> for T
impl<T> AlignerFor<64> for T
Source§impl<T> AlignerFor<8> for T
impl<T> AlignerFor<8> for T
Source§impl<T> AlignerFor<8192> for T
impl<T> AlignerFor<8192> for T
Source§type Aligner = AlignTo8192<T>
type Aligner = AlignTo8192<T>
AlignTo* type which aligns Self to ALIGNMENT.Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T, W> HasTypeWitness<W> for Twhere
W: MakeTypeWitness<Arg = T>,
T: ?Sized,
impl<T, W> HasTypeWitness<W> for Twhere
W: MakeTypeWitness<Arg = T>,
T: ?Sized,
Source§impl<T> Identity for Twhere
T: ?Sized,
impl<T> Identity for Twhere
T: ?Sized,
Source§impl<'a, T> RCowCompatibleRef<'a> for Twhere
T: Clone + 'a,
impl<'a, T> RCowCompatibleRef<'a> for Twhere
T: Clone + 'a,
Source§fn as_c_ref(from: &'a T) -> <T as RCowCompatibleRef<'a>>::RefC
fn as_c_ref(from: &'a T) -> <T as RCowCompatibleRef<'a>>::RefC
Source§fn as_rust_ref(from: <T as RCowCompatibleRef<'a>>::RefC) -> &'a T
fn as_rust_ref(from: <T as RCowCompatibleRef<'a>>::RefC) -> &'a T
Source§impl<S> ROExtAcc for S
impl<S> ROExtAcc for S
Source§fn f_get<F>(&self, offset: FieldOffset<S, F, Aligned>) -> &F
fn f_get<F>(&self, offset: FieldOffset<S, F, Aligned>) -> &F
offset. Read moreSource§fn f_get_mut<F>(&mut self, offset: FieldOffset<S, F, Aligned>) -> &mut F
fn f_get_mut<F>(&mut self, offset: FieldOffset<S, F, Aligned>) -> &mut F
offset. Read moreSource§fn f_get_ptr<F, A>(&self, offset: FieldOffset<S, F, A>) -> *const F
fn f_get_ptr<F, A>(&self, offset: FieldOffset<S, F, A>) -> *const F
offset. Read moreSource§fn f_get_mut_ptr<F, A>(&mut self, offset: FieldOffset<S, F, A>) -> *mut F
fn f_get_mut_ptr<F, A>(&mut self, offset: FieldOffset<S, F, A>) -> *mut F
offset. Read moreSource§impl<S> ROExtOps<Aligned> for S
impl<S> ROExtOps<Aligned> for S
Source§fn f_replace<F>(&mut self, offset: FieldOffset<S, F, Aligned>, value: F) -> F
fn f_replace<F>(&mut self, offset: FieldOffset<S, F, Aligned>, value: F) -> F
offset) with value,
returning the previous value of the field. Read moreSource§fn f_get_copy<F>(&self, offset: FieldOffset<S, F, Aligned>) -> Fwhere
F: Copy,
fn f_get_copy<F>(&self, offset: FieldOffset<S, F, Aligned>) -> Fwhere
F: Copy,
Source§impl<S> ROExtOps<Unaligned> for S
impl<S> ROExtOps<Unaligned> for S
Source§fn f_replace<F>(&mut self, offset: FieldOffset<S, F, Unaligned>, value: F) -> F
fn f_replace<F>(&mut self, offset: FieldOffset<S, F, Unaligned>, value: F) -> F
offset) with value,
returning the previous value of the field. Read moreSource§fn f_get_copy<F>(&self, offset: FieldOffset<S, F, Unaligned>) -> Fwhere
F: Copy,
fn f_get_copy<F>(&self, offset: FieldOffset<S, F, Unaligned>) -> Fwhere
F: Copy,
Source§impl<T> SelfOps for Twhere
T: ?Sized,
impl<T> SelfOps for Twhere
T: ?Sized,
Source§fn piped<F, U>(self, f: F) -> U
fn piped<F, U>(self, f: F) -> U
Source§fn piped_ref<'a, F, U>(&'a self, f: F) -> Uwhere
F: FnOnce(&'a Self) -> U,
fn piped_ref<'a, F, U>(&'a self, f: F) -> Uwhere
F: FnOnce(&'a Self) -> U,
piped except that the function takes &Self
Useful for functions that take &Self instead of Self. Read moreSource§fn piped_mut<'a, F, U>(&'a mut self, f: F) -> Uwhere
F: FnOnce(&'a mut Self) -> U,
fn piped_mut<'a, F, U>(&'a mut self, f: F) -> Uwhere
F: FnOnce(&'a mut Self) -> U,
piped, except that the function takes &mut Self.
Useful for functions that take &mut Self instead of Self.Source§fn mutated<F>(self, f: F) -> Self
fn mutated<F>(self, f: F) -> Self
Source§fn observe<F>(self, f: F) -> Self
fn observe<F>(self, f: F) -> Self
Source§fn as_ref_<T>(&self) -> &T
fn as_ref_<T>(&self) -> &T
AsRef,
using the turbofish .as_ref_::<_>() syntax. Read more