radn-rs/src/rstd/local_origin.rs
2023-06-30 20:19:16 +00:00

70 lines
1.8 KiB
Rust

use std::rc::Rc;
use crate::func::context::*;
use crate::rcore::*;
use super::singular::*;
use super::*;
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> Point<'a, Ctx, A> {
fn prepare_bytes_for_hashing(mentioned: &A) -> Vec<u8> {
let mut vec = mentioned.topology().to_vec();
mentioned.serialize(&mut vec);
vec
}
fn from_fields(point: Hash, origin: Rc<dyn Origin<'a, Ctx, Mtbl = A>>) -> Self {
Point { point, origin }
}
fn from_values<O: Origin<'a, Ctx, Mtbl = A>>(point: Hash, origin: O) -> Self {
Self::from_fields(point, Rc::new(origin))
}
fn from_mentionable(mentionable: Rc<A>) -> Self {
Self::from_values(
Ctx::hash(&Self::prepare_bytes_for_hashing(&mentionable)),
LocalOrigin::from(mentionable),
)
}
}
struct LocalOrigin<A>(Rc<A>);
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> Origin<'a, Ctx> for LocalOrigin<A> {
type Mtbl = A;
fn factory(&self) -> A::Fctr {
self.0.factory()
}
fn resolve(self: Rc<Self>) -> Resolution<'a, Ctx, Self::Mtbl> {
Ctx::pure(Ok(self.0.clone()))
}
fn resolve_bytes(self: Rc<Self>) -> HashResolution<'a, Ctx> {
Ctx::pure(Ok((
self.0.bytes(),
Rc::new(SingularResolver::from_mentionable(self.0.as_ref())),
)))
}
}
impl<A> From<Rc<A>> for LocalOrigin<A> {
fn from(value: Rc<A>) -> Self {
LocalOrigin(value)
}
}
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> From<Rc<A>> for Point<'a, Ctx, A> {
fn from(value: Rc<A>) -> Self {
Self::from_mentionable(value)
}
}
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> From<A> for Point<'a, Ctx, A> {
fn from(value: A) -> Self {
Self::from_mentionable(value.into())
}
}