[][src]Struct abin::AnyStr

pub struct AnyStr<TBin>(_);

A utf-8 string backed by AnyBin (Bin or SBin), see also Str and SStr.

Implementations

impl<TBin> AnyStr<TBin> where
    TBin: AnyBin
[src]

pub fn from_utf8(value: impl Into<TBin>) -> Result<Self, AnyStrUtf8Error<TBin>>[src]

Converts the given value to a string.

The given value must be valid UTF-8. If the value is not valid UTF-8, this method returns an error containing the original binary.

See also: core::str::from_utf8 / std::string::String::from_utf8.

use abin::{NewBin, BinFactory, AnyStr, Bin};
let bin : Bin = NewBin::from_static(&[65u8, 66u8, 67u8]);
let str : AnyStr<Bin> = AnyStr::from_utf8(bin).unwrap();
assert_eq!("ABC", str.as_str());

pub unsafe fn from_utf8_unchecked(value: TBin) -> Self[src]

Creates a new string from given binary without checking whether the data in the given binary is valid UTF-8.

Safety

This function is unsafe because it does not check that the bytes passed to it are valid UTF-8. If this constraint is violated, undefined behavior results, as AnyStr assumes that it only contains valid UTF-8. See also core::str::from_utf8_unchecked.

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

Returns &str. It's a cheap operation. See AnyBin::as_slice.

use abin::{Str, NewStr, StrFactory};
let string : Str = NewStr::from_static("Hello");
assert_eq!("Hello", string.as_str());

pub fn into_bin(self) -> TBin[src]

Extracts the wrapped binary.

use abin::{Str, NewStr, StrFactory, AnyBin, Bin};
let string : Str = NewStr::from_static("ABC");
let bin : Bin = string.into_bin();
assert_eq!(&[65u8, 66u8, 67u8], bin.as_slice());

pub fn as_bin(&self) -> &TBin[src]

Wrapped binary as reference.

use abin::{Str, NewStr, StrFactory, AnyBin, Bin};
let string : Str = NewStr::from_static("ABC");
let bin : &Bin = string.as_bin();
assert_eq!(&[65u8, 66u8, 67u8], bin.as_slice());

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

true if this string is empty (number of utf-8 bytes is 0). See also AnyBin::is_empty.

use abin::{Str, NewStr, StrFactory};
let string : Str = NewStr::from_static("");
assert_eq!(true, string.is_empty());

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

The number of utf-8 bytes in this string. See also AnyBin::len.

use abin::{Str, NewStr, StrFactory};
let string : Str = NewStr::from_static("Hello");
assert_eq!(5, string.len());

pub fn into_string(self) -> String[src]

Converts this into a String; depending on the type, this might allocate or not. See also AnyBin::into_vec.

use abin::{Str, NewStr, StrFactory};
let string : Str = NewStr::from_static("Hello");
let std_string : String = string.into_string();
assert_eq!("Hello", &std_string);

pub fn slice<TRange>(&self, range: TRange) -> Option<Self> where
    TRange: RangeBounds<usize>, 
[src]

Returns a slice of this string.

Returns None if:

  • range is ouf of bounds.
  • or if the range does not lie on UTF-8 boundaries (see also str::get).

See also AnyBin::slice.

use abin::{NewStr, StrFactory, Str};
let str : Str = NewStr::from_static("🗻∈🌏");

// works
let slice1 : Option<Str> = str.slice(0..4);
assert_eq!(Some(NewStr::from_static("🗻")), slice1);

// indices not on UTF-8 sequence boundaries
let slice2 : Option<Str> = str.slice(1..);
assert!(slice2.is_none());
let slice3 : Option<Str> = str.slice(..8);
assert!(slice3.is_none());
// out of bounds
let slice4 : Option<Str> = str.slice(..42);
assert!(slice4.is_none());

Trait Implementations

impl<TBin> AsRef<TBin> for AnyStr<TBin> where
    TBin: AnyBin
[src]

impl<TBin> AsRef<str> for AnyStr<TBin> where
    TBin: AnyBin
[src]

impl BooToOwned<str, AnyStr<Bin>> for NewStr[src]

impl BooToOwned<str, AnyStr<SBin>> for NewSStr[src]

impl<TBin> Borrow<str> for AnyStr<TBin> where
    TBin: AnyBin
[src]

impl<TBin> Clone for AnyStr<TBin> where
    TBin: AnyBin
[src]

impl<TBin> Debug for AnyStr<TBin> where
    TBin: AnyBin
[src]

impl<TBin> Deref for AnyStr<TBin> where
    TBin: AnyBin
[src]

type Target = str

The resulting type after dereferencing.

impl<TBin> Display for AnyStr<TBin> where
    TBin: AnyBin
[src]

impl<TBin> Eq for AnyStr<TBin> where
    TBin: AnyBin
[src]

impl<'a, TBin: AnyBin> From<AnyStr<TBin>> for StrSegment<'a, TBin>[src]

impl<TBin> Hash for AnyStr<TBin> where
    TBin: AnyBin
[src]

impl<TBin> Into<String> for AnyStr<TBin> where
    TBin: AnyBin
[src]

See AnyStr::into_string.

impl<TBin> IntoSync for AnyStr<TBin> where
    TBin: AnyBin
[src]

type Target = AnyStr<SBin>

impl<TBin> IntoUnSync for AnyStr<TBin> where
    TBin: AnyBin
[src]

type Target = AnyStr<Bin>

impl<TBin> IntoUnSyncView for AnyStr<TBin> where
    TBin: AnyBin
[src]

type Target = AnyStr<Bin>

impl<TBin> Ord for AnyStr<TBin> where
    TBin: AnyBin
[src]

impl<TBin> PartialEq<AnyStr<TBin>> for AnyStr<TBin> where
    TBin: AnyBin
[src]

impl<TBin> PartialEq<str> for AnyStr<TBin> where
    TBin: AnyBin
[src]

impl<TBin> PartialOrd<AnyStr<TBin>> for AnyStr<TBin> where
    TBin: AnyBin
[src]

impl<TBin> PartialOrd<str> for AnyStr<TBin> where
    TBin: AnyBin
[src]

Auto Trait Implementations

impl<TBin> RefUnwindSafe for AnyStr<TBin> where
    TBin: RefUnwindSafe

impl<TBin> Send for AnyStr<TBin> where
    TBin: Send

impl<TBin> Sync for AnyStr<TBin> where
    TBin: Sync

impl<TBin> Unpin for AnyStr<TBin> where
    TBin: Unpin

impl<TBin> UnwindSafe for AnyStr<TBin> where
    TBin: UnwindSafe

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.