#![doc(html_root_url = "https://docs.rs/kwap-common/0.7.0")]
#![cfg_attr(all(not(test), feature = "no_std"), no_std)]
#![cfg_attr(not(test), forbid(missing_debug_implementations, unreachable_pub))]
#![cfg_attr(not(test), deny(unsafe_code, missing_copy_implementations))]
#![allow(clippy::unused_unit)]
#![deny(missing_docs)]
extern crate alloc;
use alloc::vec::Vec;
use core::ops::{Deref, DerefMut};
pub mod result;
pub mod fns;
pub mod map;
pub use map::*;
pub mod prelude {
pub use fns::*;
pub use result::*;
pub use super::*;
}
pub trait Array:
Default
+ GetSize
+ Reserve
+ Deref<Target = [<Self as Array>::Item]>
+ DerefMut
+ Extend<<Self as Array>::Item>
+ FromIterator<<Self as Array>::Item>
+ IntoIterator<Item = <Self as Array>::Item>
{
type Item;
fn insert_at(&mut self, index: usize, value: <Self as Array>::Item);
fn remove(&mut self, index: usize) -> Option<<Self as Array>::Item>;
fn push(&mut self, value: <Self as Array>::Item);
}
impl<T> Array for Vec<T> {
type Item = T;
fn insert_at(&mut self, index: usize, value: T) {
self.insert(index, value);
}
fn remove(&mut self, index: usize) -> Option<T> {
if index < self.len() {
Some(Vec::remove(self, index))
} else {
None
}
}
fn push(&mut self, value: T) {
self.push(value)
}
}
#[derive(Clone, Copy, Debug, Default)]
pub struct Writable<A: Array<Item = u8>>(A);
impl<A: Array<Item = u8>> Writable<A> {
pub fn as_str(&self) -> &str {
core::str::from_utf8(self).unwrap()
}
}
impl<A: Array<Item = u8>> Deref for Writable<A> {
type Target = A;
fn deref(&self) -> &A {
&self.0
}
}
impl<A: Array<Item = u8>> DerefMut for Writable<A> {
fn deref_mut(&mut self) -> &mut A {
&mut self.0
}
}
impl<A: Array<Item = u8>> AsRef<str> for Writable<A> {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl<A: Array<Item = u8>> core::fmt::Write for Writable<A> {
fn write_str(&mut self, s: &str) -> core::fmt::Result {
match self.0.max_size() {
| Some(max) if max < self.len() + s.len() => Err(core::fmt::Error),
| _ => {
self.extend(s.bytes());
Ok(())
},
}
}
}
impl<A: tinyvec::Array<Item = T>, T> Array for tinyvec::ArrayVec<A> {
type Item = T;
fn insert_at(&mut self, index: usize, value: A::Item) {
self.insert(index, value);
}
fn remove(&mut self, index: usize) -> Option<T> {
if index < self.len() {
Some(tinyvec::ArrayVec::remove(self, index))
} else {
None
}
}
fn push(&mut self, value: A::Item) {
self.push(value)
}
}
pub trait GetSize {
fn get_size(&self) -> usize;
fn max_size(&self) -> Option<usize>;
fn size_is_zero(&self) -> bool {
self.get_size() == 0
}
fn is_full(&self) -> bool {
self.max_size()
.map(|max| self.get_size() >= max)
.unwrap_or(false)
}
}
impl<T> GetSize for Vec<T> {
fn get_size(&self) -> usize {
self.len()
}
fn max_size(&self) -> Option<usize> {
None
}
}
impl<A: tinyvec::Array> GetSize for tinyvec::ArrayVec<A> {
fn get_size(&self) -> usize {
self.len()
}
fn max_size(&self) -> Option<usize> {
Some(A::CAPACITY)
}
}
pub trait Reserve: Default {
fn reserve(_: usize) -> Self {
Default::default()
}
}
impl<T> Reserve for Vec<T> {
fn reserve(n: usize) -> Self {
Self::with_capacity(n)
}
}
impl<A: tinyvec::Array> Reserve for tinyvec::ArrayVec<A> {}