daemonic_error 0.2.0

Errors that compose, predict, and leave receipts - Compose: algebraic combination (in active development) - Predict: Glass/Severity - Receipts: audit trail, position, checksum - Reflection: Runtime Reflection through TopologySegment (in active development)
use alloc::rc::Rc;
use alloc::vec::Vec;
use core::fmt;
use crate::daemonic::AnchorDomainSet;
use crate::daemonic::frame::ReferenceFrame;
use crate::{Glass, TopologyAnchor, TopologySegment};
use crate::daemonic::daemonic_hasher::{DaemonicHashable, DaemonicHasher};
use super::*;
/// DaemonicBuf is a reimplemnenation over the std provided Buf, needed for DaemonicPathBuf to work correctly.
#[repr(transparent)]
pub struct DaemonicBuf {
	pub inner: Vec<u8>,
}
const DAEMONICBUF_TOPOLOGY: TopologySegment = TopologySegment {
	label: "Daemonic::Glass::Consumer::DaemonicBuf",
	hash: crate::const_daemonic_hash(
		"Daemonic::Glass::Consumer::DaemonicBuf".as_bytes(),
		crate::AXIOM_OFFSET,
	),
	crypto_id: crate::const_daemonic_hash(
		"Daemonic::Glass::Consumer::DaemonicBuf".as_bytes(),
		crate::TOPOLOGY_ANCHOR,
	),
	depth: 3u16,
};
impl crate::daemonic::Anchor for DaemonicBuf {
	fn anchor_domains(&self) -> AnchorDomainSet {
		AnchorDomainSet::SPATIAL
			.union(AnchorDomainSet::STRUCTURAL)
			.union(AnchorDomainSet::SYMBOLIC)
	}
}
impl crate::daemonic::SymbolicAnchor for DaemonicBuf {
	type Anchor = TopologySegment;
	fn meaningful_within(&self) -> impl ReferenceFrame {
		DAEMONICBUF_TOPOLOGY.clone()
	}
}
impl crate::daemonic::SemanticAnchor for DaemonicBuf {}
impl crate::daemonic::SpatialAnchor for DaemonicBuf {
	fn anchor_position(&self) -> &TopologySegment {
		&DAEMONICBUF_TOPOLOGY
	}
	fn neighbors(&self) -> TopologySegment {
		DAEMONICBUF_TOPOLOGY.clone()
	}
}
impl crate::daemonic::StructuralAnchor for DaemonicBuf {
	fn contract_description(&self) -> &str {
		"DaemonicBuf: auto-derived Daemonic anchor via #[daemonic] proc macro"
	}
}
impl crate::TopologyAnchor for DaemonicBuf {
	fn parent(&self) -> Option<TopologySegment> {
		let label = DAEMONICBUF_TOPOLOGY.label;
		label
			.rsplit_once("::")
			.map(|(parent, _)| <TopologySegment as TopologyAnchor>::new_anchor(parent))
	}
	fn children(&self) -> &[TopologySegment] {
		&[]
	}
	fn segments(&self) -> &TopologySegment {
		&DAEMONICBUF_TOPOLOGY
	}
	fn depth(&self) -> usize {
		DAEMONICBUF_TOPOLOGY.depth as usize
	}
}
impl DaemonicHashable for DaemonicBuf {
	fn declare_hashable<GLASS: DaemonicHasher<GLASS> + Glass<GLASS>>(&self, state: &mut GLASS)
	{
		DaemonicHashable::declare_hashable(&self.inner, state);
	}
}
impl IntoInner<Vec<u8>> for DaemonicBuf {
	fn into_inner(self) -> Vec<u8> {
		self.inner
	}
}

impl FromInner<Vec<u8>> for DaemonicBuf {
	fn from_inner(inner: Vec<u8>) -> Self {
		DaemonicBuf { inner }
	}
}

impl AsInner<[u8]> for DaemonicBuf {
	#[inline]
	fn as_inner(&self) -> &[u8] {
		&self.inner
	}
}

impl fmt::Debug for DaemonicBuf {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		fmt::Debug::fmt(self.as_slice(), f)
	}
}

impl fmt::Display for DaemonicBuf {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		fmt::Display::fmt(self.as_slice(), f)
	}
}
impl Clone for DaemonicBuf {
	#[inline]
	fn clone(&self) -> Self {
		DaemonicBuf { inner: self.inner.clone() }
	}
	
	#[inline]
	fn clone_from(&mut self, source: &Self) {
		self.inner.clone_from(&source.inner)
	}
}
impl DaemonicBuf {
	#[inline]
	pub fn into_encoded_bytes(self) -> Vec<u8> {
		self.inner
	}
	
	#[inline]
	pub unsafe fn from_encoded_bytes_unchecked(s: Vec<u8>) -> Self {
		Self { inner: s }
	}
	
	#[inline]
	pub fn into_string(self) -> Result<String, DaemonicBuf> {
		String::from_utf8(self.inner).map_err(|p| DaemonicBuf { inner: p.into_bytes() })
	}
	
	#[inline]
	pub fn from_string(s: String) -> DaemonicBuf {
		DaemonicBuf { inner: s.into_bytes() }
	}
	
	#[inline]
	pub fn with_capacity(capacity: usize) -> DaemonicBuf {
		DaemonicBuf { inner: Vec::with_capacity(capacity) }
	}
	
	#[inline]
	pub fn clear(&mut self) {
		self.inner.clear()
	}
	
	#[inline]
	pub fn capacity(&self) -> usize {
		self.inner.capacity()
	}
	
	#[inline]
	pub fn push_slice(&mut self, s: &DaemonicSlice) {
		self.inner.extend_from_slice(&s.inner)
	}
	
	#[inline]
	pub fn push_str(&mut self, s: &str) {
		self.inner.extend_from_slice(s.as_bytes());
	}
	
	#[inline]
	pub fn reserve(&mut self, additional: usize) {
		self.inner.reserve(additional)
	}
	
	#[inline]
	pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
		self.inner.try_reserve(additional)
	}
	
	#[inline]
	pub fn reserve_exact(&mut self, additional: usize) {
		self.inner.reserve_exact(additional)
	}
	
	#[inline]
	pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError> {
		self.inner.try_reserve_exact(additional)
	}
	
	#[inline]
	pub fn shrink_to_fit(&mut self) {
		self.inner.shrink_to_fit()
	}
	
	#[inline]
	pub fn shrink_to(&mut self, min_capacity: usize) {
		self.inner.shrink_to(min_capacity)
	}
	
	#[inline]
	pub fn as_slice(&self) -> &DaemonicSlice {
		// SAFETY: Slice just wraps [u8],
		// and &*self.inner is &[u8], therefore
		// transmuting &[u8] to &Slice is safe.
		unsafe { core::mem::transmute(self.inner.as_slice()) }
	}
	
	#[inline]
	pub fn as_mut_slice(&mut self) -> &mut DaemonicSlice {
		// SAFETY: Slice just wraps [u8],
		// and &mut *self.inner is &mut [u8], therefore
		// transmuting &mut [u8] to &mut Slice is safe.
		unsafe { core::mem::transmute(self.inner.as_mut_slice()) }
	}
	
	#[inline]
	pub fn leak<'a>(self) -> &'a mut DaemonicSlice {
		unsafe { core::mem::transmute(self.inner.leak()) }
	}
	
	#[inline]
	pub fn into_box(self) -> Box<DaemonicSlice> {
		unsafe { core::mem::transmute(self.inner.into_boxed_slice()) }
	}
	
	#[inline]
	pub fn from_box(boxed: Box<DaemonicSlice>) -> DaemonicBuf {
		let inner: Box<[u8]> = unsafe { core::mem::transmute(boxed) };
		DaemonicBuf { inner: inner.into_vec() }
	}
	
	#[inline]
	pub fn into_arc(&self) -> alloc::sync::Arc<DaemonicSlice> {
		self.as_slice().into_arc()
	}
	
	#[inline]
	pub fn into_rc(&self) -> Rc<DaemonicSlice> {
		self.as_slice().into_rc()
	}
	
	/// Provides plumbing to `Vec::truncate` without giving full mutable access
	/// to the `Vec`.
	///
	/// # Safety
	///
	/// The length must be at an `OsStr` boundary, according to
	/// `Slice::check_public_boundary`.
	#[inline]
	pub unsafe fn truncate_unchecked(&mut self, len: usize) {
		self.inner.truncate(len);
	}
	
	/// Provides plumbing to `Vec::extend_from_slice` without giving full
	/// mutable access to the `Vec`.
	///
	/// # Safety
	///
	/// This encoding has no safety requirements.
	#[inline]
	pub unsafe fn extend_from_slice_unchecked(&mut self, other: &[u8]) {
		self.inner.extend_from_slice(other);
	}
}