#[repr(C)]pub struct BSString<A = TESGlobalAlloc>where
A: SelflessAllocator,{ /* private fields */ }
Expand description
BSString
represents a string that stores its data as raw bytes from FFI with an undefined encoding.
It provides methods for managing the string’s data and converting it to/from CStr
.
This struct is designed for situations where string encoding isn’t guaranteed, such as when dealing with FFI.
§Encoding
It has been confirmed that this string is also UTF-8 when esp. etc. are saved in UTF-8.
§Examples
let mut bs = BSString::new();
assert_eq!(bs.len(), 0);
bs.set_c_str(&c"Hello, Rust!");
assert_eq!(bs.len(), 13);
assert_eq!(bs.as_c_str().to_str(), Ok("Hello, Rust!"));
Implementations§
Source§impl<A> BSString<A>where
A: SelflessAllocator,
impl<A> BSString<A>where
A: SelflessAllocator,
Sourcepub const fn new_in() -> Self
pub const fn new_in() -> Self
Creates a new, empty BSString
.
§Examples
use commonlibsse_ng::re::BSString::BSString;
use stdx::alloc::Global;
let bs = BSString::<Global>::new_in();
assert!(bs.is_empty());
Sourcepub fn from_c_str(s: &CStr) -> Result<Self, BSStringError>
pub fn from_c_str(s: &CStr) -> Result<Self, BSStringError>
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the string’s data, resetting its size and capacity.
§Examples
let mut bs = BSString::new();
bs.set_c_str(&c"Hello, Rust!");
assert!(!bs.is_empty());
bs.clear();
assert!(bs.is_empty());
Sourcepub fn set_c_str(&mut self, cstr: &CStr) -> Result<(), BSStringError>
pub fn set_c_str(&mut self, cstr: &CStr) -> Result<(), BSStringError>
Sets the content of the BSString
from a CStr
.
This method will overwrite the current data, resizing if necessary.
§Examples
let mut bs = BSString::new();
bs.set_c_str(&c"Hello, Rust!");
assert_eq!(bs.len(), 13);
§Errors
- If the string is too long to fit in a
u16
, or if allocation fails. - If allocations fail.
Sourcepub const fn len(&self) -> usize
pub const fn len(&self) -> usize
Returns the bytes length of the string.
§Examples
let mut bs = BSString::new();
bs.set_c_str(&c"Hello"); // Contains `\0`
assert_eq!(bs.len(), 6);
Sourcepub const fn capacity(&self) -> u16
pub const fn capacity(&self) -> u16
Returns the capacity of the string.
§Examples
let mut bs = BSString::new();
bs.set_c_str(&c"Hello");
assert!(bs.capacity() >= 5);
Sourcepub const fn as_bytes_with_null(&self) -> &[u8] ⓘ
pub const fn as_bytes_with_null(&self) -> &[u8] ⓘ
Returns the underlying bytes of the string, including the null terminator.
§Examples
let mut bs = BSString::new();
bs.set_c_str(&c"Hello");
assert_eq!(bs.as_bytes_with_null(), b"Hello\0");
Trait Implementations§
Source§impl<A> Clone for BSString<A>where
A: SelflessAllocator,
impl<A> Clone for BSString<A>where
A: SelflessAllocator,
Source§impl<A> Debug for BSString<A>where
A: SelflessAllocator,
impl<A> Debug for BSString<A>where
A: SelflessAllocator,
Source§impl<A> Drop for BSString<A>where
A: SelflessAllocator,
impl<A> Drop for BSString<A>where
A: SelflessAllocator,
Source§impl<A> Hash for BSString<A>where
A: SelflessAllocator,
impl<A> Hash for BSString<A>where
A: SelflessAllocator,
Source§impl<A> Ord for BSString<A>where
A: SelflessAllocator,
impl<A> Ord for BSString<A>where
A: SelflessAllocator,
Source§impl<A, B> PartialEq<BSString<B>> for BSString<A>where
A: SelflessAllocator,
B: SelflessAllocator,
impl<A, B> PartialEq<BSString<B>> for BSString<A>where
A: SelflessAllocator,
B: SelflessAllocator,
Source§impl<A, B> PartialOrd<BSString<B>> for BSString<A>where
A: SelflessAllocator,
B: SelflessAllocator,
impl<A, B> PartialOrd<BSString<B>> for BSString<A>where
A: SelflessAllocator,
B: SelflessAllocator,
impl<A> Eq for BSString<A>where
A: SelflessAllocator,
Auto Trait Implementations§
impl<A> Freeze for BSString<A>
impl<A> RefUnwindSafe for BSString<A>where
A: RefUnwindSafe,
impl<A> Send for BSString<A>where
A: Send,
impl<A> Sync for BSString<A>where
A: Sync,
impl<A> Unpin for BSString<A>where
A: Unpin,
impl<A> UnwindSafe for BSString<A>where
A: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more