//! A framework for strongly typed manipulation of weakly typed data. //! //! [![Zulip Chat](https://img.shields.io/endpoint?label=chat&url=https%3A%2F%2Fiteration-square-automation.schichler.dev%2F.netlify%2Ffunctions%2Fstream_subscribers_shield%3Fstream%3Dproject%252Ffaible)](https://iteration-square.schichler.dev/#narrow/stream/project.2Ffaible) #![doc(html_root_url = "https://docs.rs/faible/0.0.1")] #![warn(clippy::pedantic, missing_docs)] #![allow(clippy::semicolon_if_nothing_returned)] #![no_std] use core::mem::{ManuallyDrop, MaybeUninit}; #[cfg(doctest)] #[doc = include_str!("../README.md")] mod readme {} pub use faible_proc_macro_definitions::faible; /// Allows specific errors to be created in generated code. pub trait Error { /// Raised iff no matching enum variant could be determined. fn no_variant_recognized() -> Self; } pub trait Faible { type Descriptor: Descriptor; fn as_strong( &self, ) -> Result<&::Strong, ::Error>; fn as_strong_mut( &mut self, ) -> Result< &mut ::Strong, ::Error, >; } pub trait Descriptor { type Weak; type Strong; type Error: crate::Error; fn strong<'a>(&self, weak: &'a Self::Weak) -> Result<&'a Self::Strong, Self::Error>; fn strong_mut<'a>(&self, weak: &'a mut Self::Weak) -> Result<&'a mut Self::Strong, Self::Error>; fn strong_into_weak(&self, strong: Self::Strong) -> Self::Weak; fn try_weak_into_strong(&self, weak: Self::Weak) -> Result; } /// # Safety /// /// `Self` must be transmutation-compatible with `T`. /// `&Self` must be transmutation-compatible with `&T`. /// `&mut Self` must be transmutation-compatible with `&mut T`. pub unsafe trait View { #[allow(missing_docs)] fn from(value: T) -> Self where Self: Sized, T: Sized, { unsafe { (&ManuallyDrop::new(value) as *const ManuallyDrop) .cast::() .read() } } #[allow(missing_docs)] fn from_ref(value: &T) -> &Self { unsafe { *(&value as *const &T).cast() } } #[allow(missing_docs)] fn from_mut(value: &mut T) -> &mut Self { unsafe { MaybeUninit::new(value).as_ptr().cast::<&mut Self>().read() } } #[allow(missing_docs)] fn into_inner(self) -> T where Self: Sized, T: Sized, { unsafe { (&ManuallyDrop::new(self) as *const ManuallyDrop) .cast::() .read() } } fn from_insertion((slot, prev): (&mut T, Option)) -> (&mut Self, Option) where Self: Sized, T: Sized, { (Self::from_mut(slot), prev.map(Self::from)) } } /// # Safety /// /// This is the identity pun. unsafe impl View for T {} pub trait FieldAccess { fn get<'a>(&self, strong: &'a Strong, name: N) -> Result<&'a T, E>; fn get_mut<'a>(&self, strong: &'a mut Strong, name: N) -> Result<&'a mut T, E>; fn set(&self, strong: &mut Strong, name: N, value: T) -> Result<(), E> where T: Sized; fn insert<'a>( &self, strong: &'a mut Strong, name: N, value: T, ) -> Result<(&'a mut T, Option), E> where T: Sized; } // impl FieldAccess for &U // where // U: FieldAccess, // { // fn get<'a>(&self, strong: &'a Strong, name: N) -> Result<&'a T, E> { // U::get(self, strong, name) // } // fn get_mut<'a>(&self, strong: &'a mut Strong, name: N) -> Result<&'a mut T, E> { // U::get_mut(self, strong, name) // } // fn set(&self, strong: &mut Strong, name: N, value: T) -> Result<(), E> // where // T: Sized, // { // U::set(self, strong, name, value) // } // fn insert<'a>( // &self, // strong: &'a mut Strong, // name: N, // value: T, // ) -> Result<(&'a mut T, Option), E> // where // T: Sized, // { // U::insert(self, strong, name, value) // } // } pub trait UnionFieldAccess { fn get<'a>(&self, strong: &'a Strong, name: N) -> Result, E>; fn get_mut<'a>(&self, strong: &'a mut Strong, name: N) -> Result, E>; fn set(&self, strong: &mut Strong, name: N, value: T) -> Result<(), E> where T: Sized; fn insert<'a>( &self, strong: &'a mut Strong, name: N, value: T, ) -> Result<(&'a mut T, Option), E> where T: Sized; } // impl UnionFieldAccess for &U // where // U: UnionFieldAccess, // { // fn get<'a>(&self, strong: &'a Strong, name: N) -> Result, E> { // U::get(self, strong, name) // } // fn get_mut<'a>(&self, strong: &'a mut Strong, name: N) -> Result, E> { // U::get_mut(self, strong, name) // } // fn set(&self, strong: &mut Strong, name: N, value: T) -> Result<(), E> // where // T: Sized, // { // U::set(self, strong, name, value) // } // fn insert<'a>( // &self, // strong: &'a mut Strong, // name: N, // value: T, // ) -> Result<(&'a mut T, Option), E> // where // T: Sized, // { // U::insert(self, strong, name, value) // } // } pub trait VariantFilter<'a, Strong: ?Sized, E, N> { type CommonRef: 'a; type CommonMut: 'a; fn common(&self, strong: &'a Strong, name: N) -> Result, E>; fn check_mut(&self, strong: &mut Strong, name: N) -> Result; fn common_mut(&self, strong: &'a mut Strong, name: N) -> Result; } // impl VariantFilter for &T // where // T: VariantFilter, // { // fn predicate(&self, strong: &Strong, name: N) -> Result { // T::predicate(self, strong, name) // } // } pub trait VariantFieldAccessRef<'a, Common: ?Sized, E, T: ?Sized, N> { fn get(&self, common: &Common, name: N) -> Result<&'a T, E>; } pub trait VariantFieldAccessMut<'a, Common: ?Sized, E, T: ?Sized, N> { fn get_mut(&self, common: &mut Common, name: N) -> Result<&'a mut T, E>; } //TODO: Get owned, set and insert. Also to-owned conversions.