radn-rs/src/rstd/atomic_object.rs
timofey bc3d33673f
All checks were successful
buildbot/cargo fmt (1.72) Build done.
buildbot/cargo clippy (1.65) Build done.
buildbot/cargo doc (1.72) Build done.
buildbot/cargo clippy (1.72) Build done.
buildbot/cargo test (1.65) Build done.
ord/eq for AtomicObject
2023-10-15 12:10:52 +00:00

114 lines
2.7 KiB
Rust

//! Provides [Atomic]-[Mentionable] interface.
use std::{marker::PhantomData, ops::Deref};
use crate::atomic::*;
use super::*;
/// Generic implementation of a [Mentionable] for [Atomic]s.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct AtomicObject<A> {
atomic: A,
}
impl<A: AtomicBase> From<A> for AtomicObject<A> {
fn from(value: A) -> Self {
Self { atomic: value }
}
}
impl<A: AtomicBase> AsRef<A> for AtomicObject<A> {
fn as_ref(&self) -> &A {
&self.atomic
}
}
impl<A: AtomicBase> Deref for AtomicObject<A> {
type Target = A;
fn deref(&self) -> &Self::Target {
&self.atomic
}
}
impl<A: AtomicBase> Serializable for AtomicObject<A> {
fn serialize(&self, serializer: &mut dyn Serializer) {
self.atomic.serialize(serializer)
}
}
impl<'a, A: AtomicBase> MentionableBase<'a> for AtomicObject<A> {
type Fctr = AtomicFactory<A>;
fn factory(&self) -> Self::Fctr {
AtomicFactory::new()
}
}
impl<'a, Ctx: Context<'a>, A: AtomicBase> MentionableTop<'a, Ctx> for AtomicObject<A> {
fn topology_hash(&self) -> Hash {
Ctx::hash(b"")
}
fn points_typed(&self, _points: &mut impl PointsVisitor<'a, Ctx>) {}
}
/// Generic implementation of a [Factory] for [Atomic]s.
pub struct AtomicFactory<A> {
_pd: PhantomData<A>,
}
impl<A> AtomicFactory<A> {
fn new() -> Self {
AtomicFactory { _pd: PhantomData }
}
}
impl<A: AtomicBase> Clone for AtomicFactory<A> {
fn clone(&self) -> Self {
Self::new()
}
}
impl<'a, A: AtomicBase> FactoryBase<'a> for AtomicFactory<A> {
type Mtbl = AtomicObject<A>;
type ParseError = A::AParseError;
}
impl<A: ParseMode> ParseMode for AtomicFactory<A> {
type Mode = A::Mode;
}
impl<'a, Ctx: Context<'a>, A: AtomicModeParse> FactoryModeParse<'a, Ctx> for AtomicFactory<A> {
fn mdeserialize<I: InCtx<'a, Ctx>>(&self, inctx: I) -> ModeResultM<'a, Self, I> {
A::ma_deserialize(inctx).map(|a| Self::map(a, From::from))
}
fn mextend(
&self,
mentionable: ExtensionSourceM<'a, Self>,
tail: &[u8],
) -> ExtensionResultM<'a, Self> {
Self::xbind(
A::ma_extend(Self::smap(mentionable, |a| a.atomic), tail),
|a| Ok(a.into()),
)
}
}
/// Extension trait to provide method-like utilities associated with [AtomicObject]s.
pub trait AtomicObjectExt: AtomicBase {
/// Shorthand for getting specific [`AtomicFactory`].
fn f() -> AtomicFactory<Self> {
AtomicFactory::new()
}
/// Shorthand for getting specific [`AtomicObject`].
fn m(self) -> AtomicObject<Self> {
self.into()
}
}
impl<A: AtomicBase> AtomicObjectExt for A {}