#[cfg(feature = "alloc")]
use std::alloc::{Allocator, Global};
use std::mem::MaybeUninit;
use generic_vec::{
raw::{Storage, StorageWithCapacity},
ArrayVec, GenericVec,
};
use crate::{OwnedString, StringBase};
#[cfg(feature = "alloc")]
pub type String32<A = Global> = OwnedString<char, Box<[MaybeUninit<char>], A>>;
pub type ArrayString32<const N: usize> = OwnedString<char, [MaybeUninit<char>; N]>;
#[cfg(feature = "alloc")]
impl String32 {
#[inline]
pub fn new() -> Self {
Self::with_storage(Box::default())
}
#[inline]
pub fn with_capacity(capacity: usize) -> Self {
Self::new_with_capacity(capacity)
}
}
#[cfg(feature = "alloc")]
impl<A: Allocator> String32<A> {
pub fn with_alloc(alloc: A) -> Self {
Self::with_storage(Box::new_uninit_slice_in(0, alloc))
}
}
impl<const N: usize> ArrayString32<N> {
#[inline]
pub fn new() -> Self {
Self {
storage: ArrayVec::new(),
}
}
}
impl<S: ?Sized + Storage<Item = char>> OwnedString<char, S> {
#[inline]
pub fn push_str32(&mut self, string: &crate::str32) {
self.storage.extend_from_slice(&string.storage)
}
#[inline]
pub fn push(&mut self, ch: char) {
self.storage.push(ch);
}
#[inline]
pub fn pop(&mut self) -> Option<char> {
self.storage.try_pop()
}
#[inline]
pub fn truncate(&mut self, new_len: usize) {
self.storage.truncate(new_len)
}
#[inline]
pub fn remove(&mut self, idx: usize) -> char {
self.storage.remove(idx)
}
#[inline]
pub fn insert(&mut self, idx: usize, ch: char) {
self.storage.insert(idx, ch);
}
#[inline]
pub fn as_mut_vec(&mut self) -> &mut GenericVec<S::Item, S> {
&mut self.storage
}
#[inline]
#[must_use = "use `.truncate()` if you don't need the other half"]
pub fn split_off<B: ?Sized + StorageWithCapacity<Item = char>>(
&mut self,
at: usize,
) -> OwnedString<char, B> {
let other = self.storage.split_off(at);
StringBase { storage: other }
}
#[inline]
pub fn clear(&mut self) {
self.storage.clear()
}
#[inline]
pub fn capacity(&self) -> usize {
self.storage.capacity()
}
}