radn-rs/src/rcore/singular.rs
timofey 59f195609e
All checks were successful
buildbot/cargo fmt (1.72) Build done.
buildbot/cargo clippy (1.65) Build done.
buildbot/cargo clippy (1.72) Build done.
buildbot/cargo doc (1.72) Build done.
buildbot/cargo test (1.65) Build done.
PointsVisitor for SingularResolution to rcore
2023-09-03 21:21:52 +00:00

63 lines
1.5 KiB
Rust

use std::fmt::Display;
use super::*;
pub trait SingularResolution<'a, Ctx: Context<'a>>: 'a + Send + Sync {
fn singular(self: Arc<Self>) -> HashResolution<'a, Ctx>;
fn s_hash(&self) -> Hash;
}
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> SingularResolution<'a, Ctx>
for Point<'a, Ctx, A>
{
fn singular(self: Arc<Self>) -> HashResolution<'a, Ctx> {
self.origin.ref_resolve_bytes()
}
fn s_hash(&self) -> Hash {
self.point
}
}
#[derive(Debug)]
pub enum SingularError {
OutOfBounds {
index: usize,
len: usize,
},
Mismatch {
index: usize,
expected: Hash,
point: Hash,
},
}
impl Display for SingularError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::OutOfBounds { index, len } => {
write!(f, "singularity out-of-bounds: {index}/{len}")
}
Self::Mismatch {
index,
expected,
point,
} => write!(
f,
"address mismatch at index {index}: {}!={}",
hex(expected),
hex(point),
),
}
}
}
impl Error for SingularError {}
impl<'a, Ctx: Context<'a>> PointsVisitor<'a, Ctx> for Vec<Arc<dyn SingularResolution<'a, Ctx>>> {
fn visit<A: Mentionable<'a, Ctx>>(&mut self, point: &Point<'a, Ctx, A>) {
self.push(Arc::new(point.clone()) as _);
}
}