Struct CFixedStr

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

This type represents a view of 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 CFixedStr. 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 CFixedStrs, 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 CFixedStr

Source

pub unsafe fn from_ptr<'a>(ptr: *const c_char, limit: usize) -> &'a CFixedStr

Cast a raw C-style buffer to a CFixedStr.

Source

pub unsafe fn from_mut_ptr<'a>( ptr: *mut c_char, limit: usize, ) -> &'a mut CFixedStr

Cast a raw C-style buffer to a mutable CFixedStr.

Source

pub fn from_str(s: &str) -> &CFixedStr

Create a CFixedStr from a string slice.

Source

pub fn from_bytes(bytes: &[u8]) -> &CFixedStr

Create a CFixedStr from a byte slice.

Source

pub fn from_bytes_mut(bytes: &mut [u8]) -> &mut CFixedStr

Create a CFixedStr from a mutable byte slice.

Source

pub fn from_c_str(c_str: &CStr) -> &CFixedStr

Create a CFixedStr from a variable length CStr.

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.

Source

pub fn into_c_fixed_string(self: Box<CFixedStr>) -> CFixedString

Converts a Box<CFixedStr> into a CFixedString without copying or allocating.

Trait Implementations§

Source§

impl AsRef<CFixedStr> for CFixedStr

Source§

fn as_ref(&self) -> &CFixedStr

Converts this type into a shared reference of the (usually inferred) input type.
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 Debug for CFixedStr

Source§

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

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

impl<'a> Default for &'a CFixedStr

Source§

fn default() -> &'a CFixedStr

Returns the “default value” for a 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 Hash for CFixedStr

Source§

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

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

impl Ord for CFixedStr

Source§

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

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

impl PartialEq for CFixedStr

Source§

fn eq(&self, other: &CFixedStr) -> 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 CFixedStr

Source§

fn partial_cmp(&self, other: &CFixedStr) -> 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 ToOwned for CFixedStr

Source§

type Owned = CFixedString

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> CFixedString

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

fn clone_into(&self, target: &mut Self::Owned)

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

impl Eq for CFixedStr

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