SingularResolver via Topology
All checks were successful
buildbot/cargo fmt (1.72) Build done.
buildbot/cargo doc (1.72) Build done.
buildbot/cargo clippy (1.72) Build done.
buildbot/cargo test (1.65) Build done.
buildbot/cargo clippy (1.65) Build done.

This commit is contained in:
AF 2023-09-03 21:37:28 +00:00
parent 2133f70b6b
commit a28bfa461e
2 changed files with 10 additions and 10 deletions

View File

@ -37,7 +37,7 @@ impl<'a, Ctx: Context<'a>> PointsVisitor<'a, Ctx> for TopoVec<'a, Ctx> {
} }
} }
pub trait Topology<'a, Ctx: Context<'a>>: 'a { pub trait Topology<'a, Ctx: Context<'a>>: 'a + Send + Sync {
fn points_count(&self) -> usize; fn points_count(&self) -> usize;
fn point_at(&self, index: usize) -> Option<Arc<dyn SingularResolution<'a, Ctx>>>; fn point_at(&self, index: usize) -> Option<Arc<dyn SingularResolution<'a, Ctx>>>;

View File

@ -18,26 +18,26 @@ use super::*;
/// # } /// # }
/// ``` /// ```
pub struct SingularResolver<'a, Ctx: Context<'a>> { pub struct SingularResolver<'a, Ctx: Context<'a>> {
points: TopoVec<'a, Ctx>, topology: Arc<dyn Topology<'a, Ctx>>,
} }
impl<'a, Ctx: Context<'a>> SingularResolver<'a, Ctx> { impl<'a, Ctx: Context<'a>> SingularResolver<'a, Ctx> {
pub fn from_mentionable<A: Mentionable<'a, Ctx>>(a: &A) -> Self { pub fn from_mentionable<A: Mentionable<'a, Ctx>>(a: &A) -> Self {
let mut points = Vec::new(); Self {
a.points_typed(&mut points); topology: a.topology(),
Self { points } }
} }
fn resolve_robust( fn try_resolve(
self: Arc<Self>, self: Arc<Self>,
address: Address, address: Address,
) -> Result<HashResolution<'a, Ctx>, SingularError> { ) -> Result<HashResolution<'a, Ctx>, SingularError> {
let point = self let point = self
.points .topology
.get(address.index) .point_at(address.index)
.ok_or(SingularError::OutOfBounds { .ok_or(SingularError::OutOfBounds {
index: address.index, index: address.index,
len: self.points.len(), len: self.topology.points_count(),
})?; })?;
if point.s_hash() != address.point { if point.s_hash() != address.point {
Err(SingularError::Mismatch { Err(SingularError::Mismatch {
@ -53,7 +53,7 @@ impl<'a, Ctx: Context<'a>> SingularResolver<'a, Ctx> {
impl<'a, Ctx: Context<'a>> Resolver<'a, Ctx> for SingularResolver<'a, Ctx> { impl<'a, Ctx: Context<'a>> Resolver<'a, Ctx> for SingularResolver<'a, Ctx> {
fn resolve(self: Arc<Self>, address: Address) -> HashResolution<'a, Ctx> { fn resolve(self: Arc<Self>, address: Address) -> HashResolution<'a, Ctx> {
self.resolve_robust(address) self.try_resolve(address)
.map_err(SingularError::into) .map_err(SingularError::into)
.map_err(Err) .map_err(Err)
.unwrap_or_else(Ctx::pure) .unwrap_or_else(Ctx::pure)