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>
impl<const N: usize> StaticCString<N>
Sourcepub const fn new() -> Self
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);Sourcepub fn len(&self) -> usize
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);pub fn is_empty(&self) -> bool
Sourcepub const fn capacity(&self) -> usize
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);Sourcepub const fn as_ptr(&self) -> *const i8
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.
Sourcepub const fn as_mut_ptr(&mut self) -> *mut i8
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.
Sourcepub fn as_bytes(&self) -> &[u8]
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.
Sourcepub fn as_mut_bytes(&mut self) -> &mut [u8]
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.
Sourcepub fn as_bytes_with_nul(&self) -> &[u8]
pub fn as_bytes_with_nul(&self) -> &[u8]
Returns the contents of this StaticCString as a slice of bytes, including the trailing null-terminator.
Sourcepub unsafe fn as_mut_bytes_with_nul(&mut self) -> &mut [u8]
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.
pub fn as_c_str(&self) -> &CStr
Source§impl<'a, const N: usize> StaticCString<N>
impl<'a, const N: usize> StaticCString<N>
Sourcepub unsafe fn from_raw_ptr(
ptr: *const i8,
) -> Result<&'a Self, NotNullTerminatedError>
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.
Sourcepub unsafe fn from_raw_mut_ptr(
ptr: *mut i8,
) -> Result<&'a mut Self, NotNullTerminatedError>
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>
impl<const M: usize, const N: usize> AddAssign<StaticCString<M>> for StaticCString<N>
Source§fn add_assign(&mut self, rhs: StaticCString<M>)
fn add_assign(&mut self, rhs: StaticCString<M>)
+= operation. Read moreSource§impl<const N: usize> Clone for StaticCString<N>
impl<const N: usize> Clone for StaticCString<N>
Source§fn clone(&self) -> StaticCString<N>
fn clone(&self) -> StaticCString<N>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more