[][src]Struct shared_string::SharedGenString

pub struct SharedGenString<R> where
    R: RefCounter
{ /* fields omitted */ }

A SharedString, generic over its reference counter.

Most likely you will only need to interact with the type definitions SharedString and SharedSyncString.

This struct is useful for parsers or other struct that hold a lot of strings which could all reference. For example a Uri Struct or if you have a string with many lines and need every line independently.

Lines example

use shared_string::SharedString;
// or SharedSyncString if `Sync` is required

let lines: Vec<_> = SharedString::from("many\nlines\nmany").lines().collect();
assert_eq!( lines[0], SharedString::from("many") );
assert_eq!( lines[1], "lines" );
assert_eq!( lines.len(), 3 );

Implementations

impl<R> SharedGenString<R> where
    R: RefCounter
[src]

pub fn new(string: String) -> Self[src]

Creates a new SharedString with the content of String.

This will convert the String into a Boxed u8 slice.

pub fn from_utf8(vec: Vec<u8>) -> Result<Self, FromUtf8Error>[src]

Convert a vector of bytes to a SharedString.

Behaves the same way as String::from_utf8.

If you are sure that the bytes are valid UTF-8, there is an unsafe method from_utf8_unchecked which behaves the same way but skips the checks.

Errors

Returns an FromUtf8Error if the bytes are not valid UTF-8 with a description were an invalid byte was found.

pub unsafe fn from_utf8_unchecked(vec: Vec<u8>) -> Self[src]

Converts a vector of bytes to a SharedString with out checking that every bytes is valid UTF-8.

Safe version from_utf8

pub fn as_bytes(&self) -> &[u8][src]

Returns a byte slice of the underlying bytes.

To get the full bytes from which this SharedString was created from use as_bytes_full.

pub fn as_full_bytes(&self) -> &[u8][src]

Return a byte slice of the bytes from which this SharedString was created.

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

Returns a string slice of the SharedString.

Example

let s = SharedString::from("foo");

assert_eq!( "foo", s.as_str() );

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

Return a string slice of the bytes from which this SharedString was created.

Example

let mut foo = SharedString::from("foobar");
let bar = foo.split_off(3);

assert_eq!( "foo", foo.as_str() );
assert_eq!( "foobar", foo.as_full_str() );

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

Returns the len of SharedString.

Example

let mut foo = SharedString::from("foobar");
let bar = foo.split_off(3);

assert_eq!( 3, foo.len() );
assert_eq!( 3, bar.len() );

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

Returns true if the length is zero, and false otherwise.

pub fn get<I>(&self, range: I) -> Option<Self> where
    I: AsRangeInclusive, 
[src]

Returns a substring of SharedString.

This is the non-panicking alternative to idx and returns None if the range is out-of-bounds or if the start or the end are not at a char_boundary.

No allocation is performed.

Example

let foobar = SharedString::from("foobar");

assert_eq!( "foo", foobar.get(..3)? );
assert_eq!( "foob", foobar.get(..=3)? );
assert_eq!( "foobar", foobar.get(..)? );
assert_eq!( "bar", foobar.get(3..)? );

pub fn idx<I>(&self, range: I) -> Self where
    I: AsRangeInclusive, 
[src]

Returns a substring of SharedString for which no allocation is performed.

Panics

Panics if the range is out-of-bounds.

Warning

This method can lead to invalid utf8 if not "split" at a char boundary.

Note

The Index Trait is not implemented because index always returns a reference and here you always received an owned type.

Example

let foobar = SharedString::from("foobar");

assert_eq!( "foo", foobar.idx(..3) );
assert_eq!( "foob", foobar.idx(..=3) );
assert_eq!( "foobar", foobar.idx(..) );
assert_eq!( "bar", foobar.idx(3..) );

pub fn into_bytes(self) -> Vec<u8>[src]

Convert SharedString to a Vec<u8>.

Tries to avoid a call to clone if the underlying data is not used by another instance of SharedString and start is at zero.

pub fn into_full_bytes(self) -> Vec<u8>[src]

Returns the underlying Bytes from which this SharedString was created.

Tries to avoid a call to clone if the underlying data is not used by another instance.

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

Convert SharedString to a String.

Tries to avoid a call to clone if the underlying data is not used by another instance of SharedString and start is at zero.

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

Returns the underlying Bytes as a String from which this SharedString was created.

Tries to avoid a call to clone if the underlying data is not used by another instance.

pub fn push(self, ch: char) -> String[src]

Pushes a char to the String returned by into_string.

If the conditions in into_string are met no clone is perfomed.

Example

let fooba = SharedString::from("fooba");
let foobar = fooba.push('r');

assert_eq!( foobar, "foobar" );

pub fn push_str(self, string: &str) -> String[src]

Pushes a string slice to the String returned by into_string.

If the conditions in into_string are met no clone is perfomed.

Example

let foo = SharedString::from("foo");
let foobar = foo.push_str("bar");

assert_eq!( foobar, "foobar" );

pub fn split_off(&mut self, at: usize) -> Self[src]

Splits the SharedString into two at the given index.

No allocation is needed.

This is O(1) because only the reference counter is increased.

Panics

Panics if at is not at a char boundary.

pub fn split(self, byte: u8) -> Split<R>

Notable traits for Split<R>

impl<R> Iterator for Split<R> where
    R: RefCounter
type Item = SharedGenString<R>;
[src]

Returns an iterator which returns for every "segment" a SharedString.

At the moment only u8 as "splitter" is supported.

u8 will be replaced when Pattern gets stabilized.

Example

let mut foobar = SharedString::from("foo bar").split(b' ');
let foo = foobar.next().unwrap();
let bar = foobar.next().unwrap();

assert_eq!( foo, "foo" );
assert_eq!( bar, "bar" );

pub fn lines(self) -> Lines<R>

Notable traits for Lines<R>

impl<R> Iterator for Lines<R> where
    R: RefCounter
type Item = SharedGenString<R>;
[src]

Returns an iterator which returns for every line a SharedString.

Be aware that this doens't behave exactly like lines.

This implementation returns an empty line at the if there is a \n byte.

Example

let mut lines = SharedString::from("foo\r\nbar\n\nbaz\n").lines();

assert_eq!( "foo", lines.next().unwrap() );
assert_eq!( "bar", lines.next().unwrap() );
assert_eq!( "", lines.next().unwrap() );
assert_eq!( "baz", lines.next().unwrap() );

assert_eq!( None, lines.next() );

Trait Implementations

impl<R> AsRef<str> for SharedGenString<R> where
    R: RefCounter
[src]

impl<R> Borrow<str> for SharedGenString<R> where
    R: RefCounter
[src]

impl<R: Clone> Clone for SharedGenString<R> where
    R: RefCounter
[src]

impl<R> Debug for SharedGenString<R> where
    R: RefCounter
[src]

impl<R> Deref for SharedGenString<R> where
    R: RefCounter
[src]

type Target = str

The resulting type after dereferencing.

impl<R> Display for SharedGenString<R> where
    R: RefCounter
[src]

impl<R> Eq for SharedGenString<R> where
    R: RefCounter
[src]

impl<R, '_> From<&'_ str> for SharedGenString<R> where
    R: RefCounter
[src]

impl<R> From<String> for SharedGenString<R> where
    R: RefCounter
[src]

impl<R> Hash for SharedGenString<R> where
    R: RefCounter
[src]

impl<R, '_> PartialEq<&'_ str> for SharedGenString<R> where
    R: RefCounter
[src]

impl PartialEq<SharedGenString<Arc<Box<[u8], Global>>>> for str[src]

impl<'_> PartialEq<SharedGenString<Arc<Box<[u8], Global>>>> for &'_ str[src]

impl<R, O> PartialEq<SharedGenString<O>> for SharedGenString<R> where
    R: RefCounter,
    O: RefCounter
[src]

impl PartialEq<SharedGenString<Rc<Box<[u8], Global>>>> for str[src]

impl<'_> PartialEq<SharedGenString<Rc<Box<[u8], Global>>>> for &'_ str[src]

impl<R> PartialEq<str> for SharedGenString<R> where
    R: RefCounter
[src]

Auto Trait Implementations

impl<R> RefUnwindSafe for SharedGenString<R> where
    R: RefUnwindSafe

impl<R> Send for SharedGenString<R> where
    R: Send

impl<R> Sync for SharedGenString<R> where
    R: Sync

impl<R> Unpin for SharedGenString<R> where
    R: Unpin

impl<R> UnwindSafe for SharedGenString<R> where
    R: 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.