[][src]Struct ustr::Ustr

#[repr(transparent)]
pub struct Ustr { /* fields omitted */ }

A handle representing a string in the global string cache.

To use, create one using Ustr::from or the ustr function. You can freely copy, destroy or send Ustrs to other threads: the underlying string is always valid in memory (and is never destroyed).

Methods

impl Ustr[src]

pub fn from(string: &str) -> Ustr[src]

Create a new Ustr from the given &str.

You can also use the ustr function

use ustr::{Ustr, ustr as u};

let u1 = Ustr::from("the quick brown fox");
let u2 = u("the quick brown fox");
assert_eq!(u1, u2);
assert_eq!(ustr::num_entries(), 1);

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

Get the cached string as a &str

use ustr::ustr as u;

let u_fox = u("the quick brown fox");
let words: Vec<&str> = u_fox.as_str().split_whitespace().collect();
assert_eq!(words, ["the", "quick", "brown", "fox"]);

pub unsafe fn as_char_ptr(&self) -> *const c_char[src]

Get the cached string as a C char*.

This includes the null terminator so is safe to pass straight to FFI.

use ustr::ustr as u;

let u_fox = u("the quick brown fox");
let len = unsafe {
    libc::strlen(u_fox.as_char_ptr())
};
assert_eq!(len, 19);

Safety

This is just passing a raw byte array with a null terminator to C. If your source string contains non-ascii bytes then this will pass them straight along with no checking. The string is immutable. That means that if you modify it across the FFI boundary then all sorts of terrible things will happen.

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

Get the length (in bytes) of this string.

pub fn precomputed_hash(&self) -> u64[src]

Get the precomputed hash for this string

pub fn to_owned(&self) -> String[src]

Get an owned String copy of this string.

Trait Implementations

impl AsRef<str> for Ustr[src]

impl Clone for Ustr[src]

impl Copy for Ustr[src]

impl Debug for Ustr[src]

impl Display for Ustr[src]

impl Eq for Ustr[src]

impl<'_> From<&'_ str> for Ustr[src]

impl From<String> for Ustr[src]

impl Hash for Ustr[src]

impl<'_> PartialEq<&'_ str> for Ustr[src]

impl PartialEq<String> for Ustr[src]

impl PartialEq<Ustr> for Ustr[src]

impl PartialOrd<Ustr> for Ustr[src]

impl Send for Ustr[src]

impl StructuralPartialEq for Ustr[src]

impl Sync for Ustr[src]

Auto Trait Implementations

impl RefUnwindSafe for Ustr

impl Unpin for Ustr

impl UnwindSafe for Ustr

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<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[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<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.