radn-rs/src/std/local_origin.rs
2023-04-23 16:25:40 +00:00

55 lines
1.3 KiB
Rust

use std::rc::Rc;
use crate::core::*;
use super::*;
impl<'a, Ctx: 'a + Context, 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: A) -> Self {
Self::from_values(
Ctx::hash(&Self::prepare_bytes_for_hashing(&mentionable)),
LocalOrigin::from(mentionable),
)
}
}
struct LocalOrigin<A>(Rc<A>);
impl<'a, Ctx: 'a + Context, 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::T::pure(Ok(self.0.clone()))
}
}
impl<A> From<A> for LocalOrigin<A> {
fn from(value: A) -> Self {
LocalOrigin(value.into())
}
}
impl<'a, Ctx: 'a + Context, A: Mentionable<'a, Ctx>> From<A> for Point<'a, Ctx, A> {
fn from(value: A) -> Self {
Self::from_mentionable(value)
}
}