SingularResolver via Topology
	
		
			
	
		
	
	
				
					
				
			This commit is contained in:
		
							parent
							
								
									2133f70b6b
								
							
						
					
					
						commit
						a28bfa461e
					
				@ -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 point_at(&self, index: usize) -> Option<Arc<dyn SingularResolution<'a, Ctx>>>;
 | 
			
		||||
 | 
			
		||||
@ -18,26 +18,26 @@ use super::*;
 | 
			
		||||
/// # }
 | 
			
		||||
/// ```
 | 
			
		||||
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> {
 | 
			
		||||
    pub fn from_mentionable<A: Mentionable<'a, Ctx>>(a: &A) -> Self {
 | 
			
		||||
        let mut points = Vec::new();
 | 
			
		||||
        a.points_typed(&mut points);
 | 
			
		||||
        Self { points }
 | 
			
		||||
        Self {
 | 
			
		||||
            topology: a.topology(),
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn resolve_robust(
 | 
			
		||||
    fn try_resolve(
 | 
			
		||||
        self: Arc<Self>,
 | 
			
		||||
        address: Address,
 | 
			
		||||
    ) -> Result<HashResolution<'a, Ctx>, SingularError> {
 | 
			
		||||
        let point = self
 | 
			
		||||
            .points
 | 
			
		||||
            .get(address.index)
 | 
			
		||||
            .topology
 | 
			
		||||
            .point_at(address.index)
 | 
			
		||||
            .ok_or(SingularError::OutOfBounds {
 | 
			
		||||
                index: address.index,
 | 
			
		||||
                len: self.points.len(),
 | 
			
		||||
                len: self.topology.points_count(),
 | 
			
		||||
            })?;
 | 
			
		||||
        if point.s_hash() != address.point {
 | 
			
		||||
            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> {
 | 
			
		||||
    fn resolve(self: Arc<Self>, address: Address) -> HashResolution<'a, Ctx> {
 | 
			
		||||
        self.resolve_robust(address)
 | 
			
		||||
        self.try_resolve(address)
 | 
			
		||||
            .map_err(SingularError::into)
 | 
			
		||||
            .map_err(Err)
 | 
			
		||||
            .unwrap_or_else(Ctx::pure)
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user