Struct re2::string::StringWrapper

source ·
pub struct StringWrapper(/* private fields */);
Expand description

Handle to a heap-allocated C++ std::string.

This is used as an output for some string search methods such as RE2::extract() which need to allocate memory dynamically. The same instance can be provided to multiple calls in order to avoid allocating a new string upon each call, and Self::as_mut_view() and Self::resize() can be used to modify the contents in between calls.

This object “owns” its string data, and upon destruction, this object will deallocate the underlying std::string data as well. Use Self::as_view() to extract a view of the underlying data in order to copy it elsewhere.

Implementations§

source§

impl StringWrapper

source

pub const fn blank() -> Self

Generate an instance without allocating any memory dynamically.

 let s = re2::string::StringWrapper::blank();
 assert_eq!(unsafe { s.as_view().as_str() }, "");
source

pub fn from_view(s: StringView<'_>) -> Self

Generate an instance which copies the bytes from the argument s.

 let s = re2::string::StringWrapper::from_view("asdf".into());
 assert_eq!(unsafe { s.as_view().as_str() }, "asdf");
source

pub fn as_view(&self) -> StringView<'_>

Generate a read-only view of the dynamically allocated memory.

 let s = re2::string::StringWrapper::from_view("asdf".into());
 assert_eq!(unsafe { s.as_view().as_str() }, "asdf");
source

pub fn as_mut_view(&mut self) -> StringMut<'_>

Generate a mutable view of the dynamically allocated memory.

 let mut s = re2::string::StringWrapper::from_view("asdf".into());
 s.as_mut_view().as_mut_slice()[2] = b'e';
 assert_eq!(unsafe { s.as_view().as_str() }, "asef");
source

pub fn resize(&mut self, len: usize)

Resize the underlying std::string storage.

If the new length is larger than the old, this method will pad the result with null bytes.

 let mut s = re2::string::StringWrapper::from_view("asdf".into());
 assert_eq!(unsafe { s.as_view().as_str() }, "asdf");
 s.resize(2);
 assert_eq!(unsafe { s.as_view().as_str() }, "as");
 s.resize(6);
 assert_eq!(unsafe { s.as_view().as_str() }, "as\0\0\0\0");
 s.resize(0);
 assert_eq!(unsafe { s.as_view().as_str() }, "");
source

pub fn clear(&mut self)

Deallocate the underlying string storage.

This method is idempotent and can be called multiple times in a row.

 let mut s = re2::string::StringWrapper::from_view("asdf".into());
 assert_eq!(unsafe { s.as_view().as_str() }, "asdf");
 s.clear();
 assert_eq!(unsafe { s.as_view().as_str() }, "");

Trait Implementations§

source§

impl Debug for StringWrapper

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Drop for StringWrapper

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.