Struct lib::LongString
source · #[repr(C)]pub struct LongString { /* private fields */ }Implementations§
source§impl LongString
impl LongString
sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Construct a new LongString with at least capacity as the capacity. Note that this
will panic in the case of an impossible allocation (e.g. capacity > isize::MAX)
pub fn as_mut_str(&mut self) -> &mut str
sourcepub fn get_sized_buf(&self) -> NonNull<[u8]>
pub fn get_sized_buf(&self) -> NonNull<[u8]>
Returns a sized buffer representing the whole buffer of the string, can be safely written to so long as utf-8 constraints are not invalidated, and the buffer is not resized
pub fn get_non_null_slice( &self, index: usize, len: usize ) -> Option<NonNull<[u8]>>
sourcepub unsafe fn get_non_null_unchecked(&self, index: usize) -> NonNull<u8>
pub unsafe fn get_non_null_unchecked(&self, index: usize) -> NonNull<u8>
get unchecked NonNull<u8> to an index in the buffer, use get_non_null for a safe
version of this function
Safety
- You must uphold
index <= self.capacity()
sourcepub fn get_non_null(&self, index: usize) -> Option<NonNull<u8>>
pub fn get_non_null(&self, index: usize) -> Option<NonNull<u8>>
returns a pointer to the element of the buffer that is at an offset of index from the
start, or None if the pointer is out of bounds
sourcepub fn next_ptr(&mut self) -> NonNull<u8>
pub fn next_ptr(&mut self) -> NonNull<u8>
returns a pointer to the next element of the buffer that we want to allocate to, note that
the pointer might not be writeable, as it could be outside of the buffer. In order to write
to the pointer, ensure that len < capacity
sourcepub const fn len(&self) -> usize
pub const fn len(&self) -> usize
returns the length of this string in bytes, length upholds the following invariants, that you needn’t check
self.len() < self.capacity()self.len() < isize::MAX(derived invariant fromself.capacity() < isize::MAX)
sourcepub const fn capacity(&self) -> usize
pub const fn capacity(&self) -> usize
Returns the capacity of this string, that is, how many bytes it can fit before a realloc.
Note that this does not mean extra bytes, but total bytes. Use remaining_capacity for
that.
self.capacity() upholds the following invariants:
self.capacity() < isize::MAXself.capacity()is the exact size of the allocated buffer
sourcepub const fn remaining_capacity(&self) -> usize
pub const fn remaining_capacity(&self) -> usize
returns the remaining capacity of this string (how many bytes we can allcoate before a realloc must occur)
sourcepub fn clone_with_additional_capacity(&self, additional_capacity: usize) -> Self
pub fn clone_with_additional_capacity(&self, additional_capacity: usize) -> Self
clones this string, with at least additional_capacity extra space
sourcepub fn realloc(&mut self, remaining_capacity: usize)
pub fn realloc(&mut self, remaining_capacity: usize)
realloc to fit at least remaining_capacity more bytes
sourcepub unsafe fn push_str_unchecked(&mut self, s: &str)
pub unsafe fn push_str_unchecked(&mut self, s: &str)
Safety
self.remaining_capacity()`` must be at leasts.len()`
sourcepub fn push_str(&mut self, s: &str)
pub fn push_str(&mut self, s: &str)
Push a str to this string, allocating if needed. Note that the current realloc schema
might only allocate exactly enough extra space for s
sourcepub fn push(&mut self, ch: char)
pub fn push(&mut self, ch: char)
Push a char to this string, allocating if needed. Like LongString::push_str this might
only allocate enough extra space for ch, but that is very unlikely in this case.
sourcepub unsafe fn from_raw_parts(
buf: NonNull<u8>,
length: usize,
capacity: usize
) -> Self
pub unsafe fn from_raw_parts( buf: NonNull<u8>, length: usize, capacity: usize ) -> Self
Construct a new LongString from a length, buf and capacity
Safety
- invariants of
length0is always a valid valuelen <= capacitybuf[0..len]is always a valid SharedReadWrite slice of valid u8, if the string is not borrowed, otherwise the permissions become that of the borrow
- invariants of
bufandcapacity- The size of the allocated object starting at buf is exactly
capacitybytes long bufmust be allocated with std::allocator::Global
- The size of the allocated object starting at buf is exactly