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 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 '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

An empty string.

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Formats the value using the given formatter. Read more

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

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

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

The owned type, stored in RCow::Owned

The borrowed type, stored in RCow::Borrowed

Performs the conversion.

This is always WithMetadata_<Self, Self>

Performs the conversion.

Gets a reference to a field, determined by offset. Read more

Gets a muatble reference to a field, determined by offset. Read more

Gets a const pointer to a field, the field is determined by offset. Read more

Gets a mutable pointer to a field, determined by offset. Read more

Replaces a field (determined by offset) with value, returning the previous value of the field. Read more

Swaps a field (determined by offset) with the same field in right. Read more

Gets a copy of a field (determined by offset). The field is determined by offset. Read more

Replaces a field (determined by offset) with value, returning the previous value of the field. Read more

Swaps a field (determined by offset) with the same field in right. Read more

Gets a copy of a field (determined by offset). The field is determined by offset. Read more

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

Drops self using method notation. Alternative to std::mem::drop. Read more

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

Transmutes the element type of this pointer.. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

This is always Self.

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.

This is supported on crate feature alloc only.

Converts a box back to the original type.

This is supported on crate feature alloc only.

Converts an Arc back to the original type. Read more

This is supported on crate feature alloc only.

Converts an Rc back to the original type. Read more

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.

This is supported on crate feature alloc only.

Converts a box back to the original type.

This is supported on crate feature alloc only.

Converts an Arc back to the original type.

This is supported on crate feature alloc only.

Converts an Rc back to the original type.