Skip to main content

OwnedCStrs

Struct OwnedCStrs 

Source
pub struct OwnedCStrs { /* private fields */ }
Expand description

A AsCStrArray backed by a collection of owned strings

This struct is a counterpart to BorrowedCStrs that owns the strings themselves instead of borrowing them. OwnedCStrs owns both the allocation for the array of pointers and the Vec of owned strings, ensuring that the pointers remain valid for the lifetime of the OwnedCStrs.

The implementation of FromIterator allows you to create an OwnedCStrs from an iterator of CStrings, which can be useful when you want to create an AsCStrArray from scratch and need to own the strings themselves.

Implementations§

Source§

impl OwnedCStrs

Source

pub fn new(values: Vec<CString>) -> Self

Creates a new OwnedCStrs from a Vec<CString>.

The given values is stored in the OwnedCStrs for the lifetime of it to keep the strings valid and unmodified. A null-terminated array of pointers to the C-style strings is created from the values and stored in the OwnedCStrs as well. The pointer to the array is returned by the as_ptr method.

Source

pub unsafe fn from_pointers_and_values( pointers: Vec<*const c_char>, values: Vec<CString>, ) -> Self

Creates a new OwnedCStrs from its inner components.

This function directly uses the given pointers as the content of the OwnedCStrs. The pointers is a null-terminated array of pointers to C-style strings that is returned by the as_ptr method. The values is a Vec<CString> stored in the OwnedCStrs for the lifetime of it.

§Safety

The caller must ensure that the pointers in the first Vec point to valid C-style strings, and that the Vec is null-terminated (i.e., the last pointer is a null pointer). The strings must remain valid and unmodified for the lifetime of the OwnedCStrs.

This function does not require that the pointers point to strings contained in values. However, if the strings pointed to by pointers are modified or dropped through other means while the OwnedCStrs is alive, it may lead to undefined behavior when the pointers are used. If the strings pointed to by pointers are not contained in values, you should consider using BorrowedCStrs instead, as it supports lifetime-based safety guarantees for the strings.

Source

pub fn into_pointers_and_values(self) -> (Vec<*const c_char>, Vec<CString>)

Consumes the OwnedCStrs and returns the owned array of pointers and values.

This function just returns its inner components. The caller is responsible for ensuring the validity of the returned pointers if they intend to use them. If self was created by OwnedCStrs::new, the C-style strings pointed to by the returned pointers will be valid until the returned values are mutated or dropped.

Trait Implementations§

Source§

impl AsCStrArray for OwnedCStrs

Source§

fn as_ptr(&self) -> *const *const c_char

Returns a pointer to the array of C-style strings. Read more
Source§

fn as_mut_ptr(&mut self) -> *const *mut c_char

Returns a pointer to the array of C-style strings. Read more
Source§

fn to_vec(&self) -> Vec<CString>

Constructs a Vec<CString> from the array of C-style strings. Read more
Source§

impl Clone for OwnedCStrs

Clones an OwnedCStrs by cloning its owned strings and reconstructing the pointer array to point to the new strings.

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for OwnedCStrs

Source§

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

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

impl From<Vec<CString>> for OwnedCStrs

Converts a Vec<CString> into an OwnedCStrs. (See OwnedCStrs::new.)

Source§

fn from(values: Vec<CString>) -> Self

Converts to this type from the input type.
Source§

impl<A> FromIterator<A> for OwnedCStrs
where A: Into<CString>,

Creates an OwnedCStrs from an iterator of values that can be converted into CStrings.

Source§

fn from_iter<I: IntoIterator<Item = A>>(iter: I) -> Self

Creates a value from an iterator. 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

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> IntoCStrArray for T
where T: AsCStrArray,

Source§

type CStrArray = T

The type that self can be converted into, which must implement AsCStrArray.
Source§

fn into_c_str_array(self) -> <T as IntoCStrArray>::CStrArray

Converts self into a type that implements AsCStrArray.
Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

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>,

Source§

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.