radn-rs/src/std.rs
2023-04-24 23:54:49 +00:00

78 lines
2.1 KiB
Rust

//! Standard extensions to [`crate::core`].
pub mod atomic;
pub mod cast;
pub mod collections;
pub mod fallible;
pub mod inlining;
mod local_origin;
pub mod nullable;
pub mod point;
pub mod tracing;
mod typeless;
use std::{error::Error, fmt::Display, rc::Rc};
use crate::core::*;
use crate::func::*;
impl Display for Address {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!("{}@{}", hex::encode(self.point), self.index))
}
}
/// Extension trait for [Serializable]s.
pub trait ExtSerializable: Serializable {
/// Serialize into a [Vec] of bytes.
fn bytes(&self) -> Vec<u8>;
}
impl<S: Serializable> ExtSerializable for S {
fn bytes(&self) -> Vec<u8> {
let mut vec = Vec::new();
self.serialize(&mut vec);
vec
}
}
/// Extention trait for simpler conversion between [`Context::T`] and [`Context::Fallible`].
///
/// Until either Rust type system or [`crate::func`] take serious changes,
/// this is the preferred way to switch between [Wrapped] and [fallible].
pub trait FallibleContext: Context {
/// Convert a fallible wrapped into a wrapped result.
fn unstuff<'a, A: 'a, E: 'a>(
wa: <<Self::Fallible as MonadFailAny>::W<E> as WeakFunctor>::F<'a, A>,
) -> <Self::T as WeakFunctor>::F<'a, Result<A, E>>
where
Self: 'a;
/// Convert a wrapped result into a fallible wrapped.
fn stuff<'a, A: 'a, E: 'a>(
fa: <Self::T as WeakFunctor>::F<'a, Result<A, E>>,
) -> <<Self::Fallible as MonadFailAny>::W<E> as WeakFunctor>::F<'a, A>
where
Self: 'a;
}
impl<Ctx: Context> FallibleContext for Ctx {
fn unstuff<'a, A: 'a, E: 'a>(
wa: <<Self::Fallible as MonadFailAny>::W<E> as WeakFunctor>::F<'a, A>,
) -> <Self::T as WeakFunctor>::F<'a, Result<A, E>>
where
Self: 'a,
{
Self::Fallible::unstuff(wa)
}
fn stuff<'a, A: 'a, E: 'a>(
fa: <Self::T as WeakFunctor>::F<'a, Result<A, E>>,
) -> <<Self::Fallible as MonadFailAny>::W<E> as WeakFunctor>::F<'a, A>
where
Self: 'a,
{
Self::Fallible::stuff(fa)
}
}