[][src]Struct abi_stable::std_types::RStr

#[repr(C)]pub struct RStr<'a> { /* fields omitted */ }

Ffi-safe equivalent of &'a str

Example

This defines a function that returns the first word in a string.

use abi_stable::{
    std_types::RStr,
    sabi_extern_fn,
};


#[sabi_extern_fn]
fn first_word(phrase:RStr<'_>)->RStr<'_>{
    match phrase.as_str().split_whitespace().next() {
        Some(x)=>x.into(),
        None=>"".into()
    }
}

Implementations

impl<'a> RStr<'a>[src]

pub const EMPTY: Self[src]

An empty RStr.

impl<'a> RStr<'a>[src]

pub const fn empty() -> Self[src]

Constructs an empty RStr<'a>.

Example

use abi_stable::std_types::RStr;

const STR:RStr<'static>=RStr::empty();

assert_eq!(STR,RStr::from(""));

pub const unsafe fn from_raw_parts(ptr_: *const u8, len: usize) -> Self[src]

Constructs an RStr<'a> from a pointer to the first byte, and a length.

Safety

Callers must ensure that:

  • ptr_ points to valid memory,

  • ptr_ .. ptr+len range is accessible memory,and is valid utf-8.

  • The data that ptr_ points to must be valid for the lifetime of this RStr<'a>

Examples

This function unsafely converts a &str to an RStr<'_>, equivalent to doing RStr::from.

use abi_stable::std_types::RStr;

fn convert(slice_:&str)->RStr<'_>{
    unsafe{
        RStr::from_raw_parts( slice_.as_ptr(), slice_.len() )
    }
}

pub const fn from_str(s: &'a str) -> Self[src]

Converts &'a str to a RStr<'a>.

Example

use abi_stable::std_types::RStr;

assert_eq!(RStr::from_str("").as_str(), "");
assert_eq!(RStr::from_str("Hello").as_str(), "Hello");
assert_eq!(RStr::from_str("World").as_str(), "World");

pub fn slice<I>(&self, i: I) -> RStr<'a> where
    str: Index<I, Output = str>, 
[src]

For slicing RStrs.

This is an inherent method instead of an implementation of the std::ops::Index trait because it does not return a reference.

Example

use abi_stable::std_types::RStr;

let str=RStr::from("What is that.");

assert_eq!(str.slice(..),str);
assert_eq!(str.slice(..4),RStr::from("What"));
assert_eq!(str.slice(4..),RStr::from(" is that."));
assert_eq!(str.slice(4..7),RStr::from(" is"));

pub const fn as_rslice(&self) -> RSlice<'a, u8>

Notable traits for RSlice<'a, u8>

impl<'a> Read for RSlice<'a, u8>
[src]

Accesses the underlying byte slice.

Example

use abi_stable::std_types::{RSlice,RStr};

let str=RStr::from("What is that.");
let bytes=RSlice::from("What is that.".as_bytes());

assert_eq!(str.as_rslice(),bytes);

pub fn as_str(&self) -> &'a str[src]

Casts this RStr<'a> to a &'a str.

Example

use abi_stable::std_types::RStr;

let str="What is that.";
assert_eq!(RStr::from(str).as_str(),str);

pub const fn as_ptr(&self) -> *const u8[src]

Gets a raw pointer to the start of the string slice.

pub const fn len(&self) -> usize[src]

Gets the length(in bytes) of this RStr<'a>.

Example

use abi_stable::std_types::RStr;

assert_eq!(RStr::from("").len(),0);
assert_eq!(RStr::from("a").len(),1);
assert_eq!(RStr::from("What").len(),4);

pub const fn is_empty(&self) -> bool[src]

Queries whether this RStr is empty.

Example

use abi_stable::std_types::RStr;

assert_eq!(RStr::from("").is_empty(),true);
assert_eq!(RStr::from("a").is_empty(),false);
assert_eq!(RStr::from("What").is_empty(),false);

Trait Implementations

impl<'_> AsRef<[u8]> for RStr<'_>[src]

impl<'_> AsRef<str> for RStr<'_>[src]

impl<'a> Borrow<str> for RStr<'a>[src]

impl<'a> Clone for RStr<'a>[src]

impl<'a> Copy for RStr<'a>[src]

impl<'a> Debug for RStr<'a>[src]

impl<'a> Default for RStr<'a>[src]

impl<'a> Deref for RStr<'a>[src]

type Target = str

The resulting type after dereferencing.

impl<'de> Deserialize<'de> for RStr<'de>[src]

impl<'_> Display for RStr<'_>[src]

impl<'a> Eq for RStr<'a>[src]

impl<'a> From<&'a str> for RStr<'a>[src]

impl<'a> From<RStr<'a>> for RCow<'a, str>[src]

impl<'a> GetStaticEquivalent_ for RStr<'a>[src]

type StaticEquivalent = _static_RStr<'static>

impl<'a> Hash for RStr<'a>[src]

impl<'a> Into<&'a str> for RStr<'a>[src]

impl<'a> Into<Cow<'a, str>> for RStr<'a>[src]

impl<'a> Into<RString> for RStr<'a>[src]

impl<'a> Into<String> for RStr<'a>[src]

impl<'a> IntoReprRust for RStr<'a>[src]

type ReprRust = &'a str

The #[repr(Rust)] equivalent.

impl<'a> Ord for RStr<'a>[src]

impl<'a> PartialEq<RStr<'a>> for RStr<'a>[src]

impl<'a> PartialOrd<RStr<'a>> for RStr<'a>[src]

impl<'a> Send for RStr<'a>[src]

impl<'a> Serialize for RStr<'a>[src]

impl<'a> StableAbi for RStr<'a>[src]

type IsNonZeroType = False

Whether this type has a single invalid bit-pattern. Read more

impl<'a> Sync for RStr<'a>[src]

Auto Trait Implementations

impl<'a> RefUnwindSafe for RStr<'a>

impl<'a> Unpin for RStr<'a>

impl<'a> UnwindSafe for RStr<'a>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<'a, T> BorrowOwned<'a> for T where
    T: 'a + Clone
[src]

type ROwned = T

The owned type, stored in RCow::Owned

type RBorrowed = &'a T

The borrowed type, stored in RCow::Borrowed

impl<T> From<T> for T[src]

impl<T> GetWithMetadata for T[src]

type ForSelf = WithMetadata_<T, T>

This is always WithMetadata_<Self, Self>

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> SelfOps for T where
    T: ?Sized
[src]

impl<T> StringExt for T where
    T: Borrow<str> + ?Sized
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<This> TransmuteElement for This where
    This: ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The error type returned when the conversion fails.

impl<T> TypeIdentity for T where
    T: ?Sized
[src]

type Type = T

The same type as Self. Read more

impl<This> ValidTag_Bounds for This where
    This: Debug + Clone + PartialEq<This>, 
[src]