Skip to main content

StaticCString

Struct StaticCString 

Source
pub struct StaticCString<const N: usize> { /* private fields */ }
Expand description

A C-compatible, growable but fixed-capacity string.
The exact encoding of the string depends on the target platform.
The StaticCString guarantees a null-terminator at the end, so the maximum length is 1 less than capacity.

Implementations§

Source§

impl<const N: usize> StaticCString<N>

Source

pub const fn new() -> Self

Creates a new empty StaticCString.

Given that the string is empty, the buffer that contains the string isn’t initialized. This means the initial operation is very inexpensive.

§Example
use static_collections::ffi::c_str::StaticCString;
let s:StaticCString<32>=StaticCString::from(c"Hello, World!");
assert_eq!(s.len(),13);
Source

pub fn len(&self) -> usize

Returns the length of the static-string by using strnlen.

§Example
use static_collections::ffi::c_str::StaticCString;
let s:StaticCString<32>=StaticCString::from(c"Hello, World!");
assert_eq!(s.len(),13);
Source

pub fn is_empty(&self) -> bool

Source

pub const fn capacity(&self) -> usize

Returns the capacity of the static-string.

§Example
use static_collections::ffi::c_str::StaticCString;
let s:StaticCString<32>=StaticCString::from(c"Hello, World!");
assert_eq!(s.capacity(),32);
Source

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

Returns the immutable pointer to the first character.

The returned pointer can be passed to C-FFI routines that accepts raw pointers.

Source

pub const fn as_mut_ptr(&mut self) -> *mut i8

Returns the mutable pointer to the first character.

The returned pointer can be passed to C-FFI routines that accepts raw pointers.

Source

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

Returns the contents of this StaticCString as a slice of bytes.

The returned slice does not contain the trailing null-terminator, and it is guaranteed to not contain any interior null bytes. If you need this null-terminator, use StaticCString::as_bytes_with_nul instead.

Source

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

Returns the contents of this StaticCString as a mutable slice of bytes.

The returned slice does not contain the trailing null-terminator, and it is guaranteed to not contain any interior null bytes. If you need this null-terminator, use StaticCString::as_mut_bytes_with_nul instead.

Source

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

Returns the contents of this StaticCString as a slice of bytes, including the trailing null-terminator.

Source

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

Returns the contents of this StaticCString as a slice of bytes, including the trailing null-terminator.

§Safety

You must ensure the slice contains a null-terminator throughout the lifetime of the slice.

Source

pub fn as_c_str(&self) -> &CStr

Source§

impl<'a, const N: usize> StaticCString<N>

Source

pub unsafe fn from_raw_ptr( ptr: *const i8, ) -> Result<&'a Self, NotNullTerminatedError>

Gets an immutable fixed-capacity StaticCString object reference from a raw pointer.

Returns Err(NotNullTerminatedError) if the string is not null-terminated.

§Safety

You must ensure the lifetime of the &'a StaticCString lives long enough.
You must ensure ptr points to a valid region that has N bytes.

§Panic

The strnlen will be called by this function. It may trigger an exception.
The program may either panic, crash, or run normally if the exception is handled.

Source

pub unsafe fn from_raw_mut_ptr( ptr: *mut i8, ) -> Result<&'a mut Self, NotNullTerminatedError>

Gets a mutable fixed-capacity StaticCString object reference from a raw pointer.

Returns Err(NotNullTerminatedError) if the string is not null-terminated.

§Safety

You must ensure the lifetime of the &'a mut StaticCString lives long enough.
You must ensure ptr points to a valid region that has N bytes.

§Panic

The strnlen will be called by this function. It may trigger an exception.
The program may either panic, crash, or run normally if the exception is handled.

Trait Implementations§

Source§

impl<const M: usize, const N: usize> AddAssign<StaticCString<M>> for StaticCString<N>

Source§

fn add_assign(&mut self, rhs: StaticCString<M>)

Performs the += operation. Read more
Source§

impl<const N: usize> Clone for StaticCString<N>

Source§

fn clone(&self) -> StaticCString<N>

Returns a duplicate 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<const N: usize> Debug for StaticCString<N>

Source§

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

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

impl<const N: usize> Default for StaticCString<N>

Source§

fn default() -> Self

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

impl<const N: usize> From<&CStr> for StaticCString<N>

Source§

fn from(value: &CStr) -> Self

Converts to this type from the input type.
Source§

impl<const M: usize, const N: usize> PartialEq<StaticCString<M>> for StaticCString<N>

Source§

fn eq(&self, other: &StaticCString<M>) -> 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<const M: usize, const N: usize> PartialOrd<StaticCString<M>> for StaticCString<N>

Source§

fn partial_cmp(&self, other: &StaticCString<M>) -> 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<const N: usize> Send for StaticCString<N>

Source§

impl<const N: usize> Sync for StaticCString<N>

Auto Trait Implementations§

§

impl<const N: usize> Freeze for StaticCString<N>

§

impl<const N: usize> RefUnwindSafe for StaticCString<N>

§

impl<const N: usize> Unpin for StaticCString<N>

§

impl<const N: usize> UnsafeUnpin for StaticCString<N>

§

impl<const N: usize> UnwindSafe for StaticCString<N>

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