Struct CFixedString

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

This type owns a fixed length buffer containing a C-style string. The string is terminated by the first null byte in the buffer, or by the length of the buffer if no null byte is present.

The length of the buffer is known as the limit of the CFixedString. The len() method returns the length of the string within the buffer.

Any bytes after the first null byte are not considered part of the string and may contain garbage data. When comparing or hashing CFixedStrings, these bytes are not included.

§Performance

Be aware that many of the methods on this type which would normally be expected to run in constant time may run in time linear in the length of the string. For this reason, it is recommended to use the type only for interop with C code, and to convert to a standard string or byte buffer as soon as possible.

Implementations§

Source§

impl CFixedString

Source

pub fn new<T: Into<Box<[u8]>>>(t: T) -> CFixedString

Construct a new fixed-length C string from a byte slice The first null byte terminates the string, but the length of the slice determines the limit of this fixed-size string.

Source

pub fn with_limit(limit: usize) -> CFixedString

Construct an empty string with the specified limit. The buffer is entirely zero-initialized.

Source

pub unsafe fn from_raw_parts(ptr: *mut c_char, limit: usize) -> CFixedString

Retakes ownership of a CFixedString that was transferred to C.

§Safety

This should only ever be called with a pointer and limit that was earlier obtained from a CFixedString. The previous string must have been forgotten to ensure that the memory has not already been freed.

Source

pub fn into_string(self) -> Result<String, IntoStringError>

Converts the CFixedString into a String if it contains valid Unicode data.

On failure, ownership of the original CFixedString is returned.

Source

pub fn into_c_string(self) -> CString

Converts the CFixedString into a CString. If necessary, the buffer will be extended to make room for a final null byte. If the buffer does not need to be extended, no allocation will be performed.

Source

pub fn into_bytes(self) -> Vec<u8>

Converts the CFixedString into a byte buffer. The length of the buffer will equal the length of the string up to but not including the first null byte. The capacity of the buffer will equal the limit of the CFixedString.

Source

pub fn into_bytes_full(self) -> Vec<u8>

Converts the CFixedString into a byte buffer. The length of the buffer will equal the limit of the CFixedString. The buffer may contain garbage values after the first null byte.

Source

pub fn as_c_fixed_str(&self) -> &CFixedStr

Extracts a CFixedStr slice containing the entire buffer.

Source

pub fn as_c_fixed_str_mut(&mut self) -> &mut CFixedStr

Extracts a mutable CFixedStr slice containing the entire buffer.

Source

pub fn into_boxed_c_fixed_str(self) -> Box<CFixedStr>

Converts this CFixedString into a boxed CFixedStr

Methods from Deref<Target = CFixedStr>§

Source

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

Returns the inner pointer to this CFixedStr.

§Safety

It is your responsibility to make sure the underlying memory is not freed to early.

Source

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

Returns the inner pointer to this mutable CFixedStr.

§Safety

It is your responsibility to make sure the underlying memory is not freed to early.

Source

pub fn limit(&self) -> usize

Returns the limit of this CFixedStr. This corresponds to the longest possible string that could be stored here.

Source

pub fn len(&self) -> usize

Returns the length of the CFixedStr. This operation takes linear time.

Source

pub fn to_bytes(&self) -> &[u8]

Coverts this CFixedStr to a byte slice. The length of the slice is equal to the length of the string up to but not including the first null byte.

Source

pub fn to_bytes_mut(&mut self) -> &mut [u8]

Coverts this CFixedStr to a mutable byte slice. The length of the slice is equal to the length of the string up to but not including the first null byte.

Source

pub fn to_bytes_extended(&self) -> &[u8]

Coverts this CFixedStr to a byte slice. The length of the slice is equal to the length of the string up to and including the first null byte, if it exists.

Source

pub fn to_bytes_mut_extended(&mut self) -> &mut [u8]

Coverts this CFixedStr to a mutable byte slice. The length of the slice is equal to the length of the string up to and including the first null byte, if it exists.

Source

pub fn as_bytes_full(&self) -> &[u8]

Coverts this CFixedStr to a byte slice. The length of the slice is equal to the limit of the CFixedStr.

Source

pub fn as_bytes_mut_full(&mut self) -> &mut [u8]

Coverts this CFixedStr to a mutable byte slice. The length of the slice is equal to the limit of the CFixedStr.

Source

pub fn to_str(&self) -> Result<&str, Utf8Error>

Yields a &str slice if the CFixedStr contains valid UTF-8.

This function will calculate the length of this string and check for UTF-8 validity, and then return the &str if it’s valid.

Source

pub fn to_string_lossy(&self) -> Cow<'_, str>

Converts a CFixedStr into a Cow.

This function will calculate the length of this string and then return the resulting slice as a Cow<str>, replacing any invalid UTF-8 sequences with U+FFFD REPLACEMENT CHARACTER.

Source

pub fn to_c_str(&self) -> Cow<'_, CStr>

Converts a CFixedStr into a Cow.

This function will calculate the length of this string, and then ensure it has a terminating null byte. If a null byte is already present, the Borrowed variant can be returned.

Trait Implementations§

Source§

impl AsRef<CFixedStr> for CFixedString

Source§

fn as_ref(&self) -> &CFixedStr

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Borrow<CFixedStr> for CFixedString

Source§

fn borrow(&self) -> &CFixedStr

Immutably borrows from an owned value. Read more
Source§

impl Clone for CFixedString

Source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for CFixedString

Source§

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

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

impl Default for CFixedString

Source§

fn default() -> CFixedString

Returns the “default value” for a type. Read more
Source§

impl Deref for CFixedString

Source§

type Target = CFixedStr

The resulting type after dereferencing.
Source§

fn deref(&self) -> &CFixedStr

Dereferences the value.
Source§

impl DerefMut for CFixedString

Source§

fn deref_mut(&mut self) -> &mut CFixedStr

Mutably dereferences the value.
Source§

impl Drop for CFixedString

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<'a> From<&'a CFixedStr> for CFixedString

Source§

fn from(s: &'a CFixedStr) -> CFixedString

Converts to this type from the input type.
Source§

impl From<CFixedString> for Vec<u8>

Source§

fn from(s: CFixedString) -> Vec<u8>

Converts to this type from the input type.
Source§

impl Hash for CFixedString

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Index<RangeFull> for CFixedString

Source§

type Output = CFixedStr

The returned type after indexing.
Source§

fn index(&self, _index: RangeFull) -> &CFixedStr

Performs the indexing (container[index]) operation. Read more
Source§

impl IndexMut<RangeFull> for CFixedString

Source§

fn index_mut(&mut self, _index: RangeFull) -> &mut CFixedStr

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl Ord for CFixedString

Source§

fn cmp(&self, other: &CFixedString) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for CFixedString

Source§

fn eq(&self, other: &CFixedString) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for CFixedString

Source§

fn partial_cmp(&self, other: &CFixedString) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Eq for CFixedString

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> 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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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.