Repo

Trait Repo 

Source
pub trait Repo {
    type Error: 'static;

    // Required method
    fn fetch<'life0, 'async_trait, D, S>(
        &'life0 self,
        id: D,
        session: S,
    ) -> Pin<Box<dyn Future<Output = Result<Option<D::Output>, Self::Error>> + 'async_trait>>
       where D: 'async_trait + Dereference,
             S: 'async_trait + Session,
             Self: 'async_trait,
             'life0: 'async_trait;
}
Expand description

A Repository from which objects can be dereferenced

This can describe both HTTP remotes, e.g. for fetching remote ActivityPub objects, as well as for fetching local objects from a database

use apub_core::{repo::{Dereference, Repo}, session::Session};
use std::{collections::HashMap, sync::{Arc, Mutex}};
use url::Url;

/// An in-memory repository for objects
pub struct MemoryRepo {
    inner: Arc<Mutex<HashMap<Url, serde_json::Value>>>,
}

#[async_trait::async_trait(?Send)]
impl Repo for MemoryRepo {
    type Error = serde_json::Error;

    async fn fetch<D: Dereference, S: Session>(&self, id: D, _: S) -> Result<Option<D::Output>, Self::Error> {
        if let Some(value) = self.inner.lock().unwrap().get(id.url()) {
            serde_json::from_value(value.clone()).map(Some)
        } else {
            Ok(None)
        }
    }
}

Required Associated Types§

Source

type Error: 'static

The Error produced by fetching an object

Required Methods§

Source

fn fetch<'life0, 'async_trait, D, S>( &'life0 self, id: D, session: S, ) -> Pin<Box<dyn Future<Output = Result<Option<D::Output>, Self::Error>> + 'async_trait>>
where D: 'async_trait + Dereference, S: 'async_trait + Session, Self: 'async_trait, 'life0: 'async_trait,

Fetch the object from the repository

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<'a, T> Repo for &'a T
where T: Repo,

Source§

type Error = <T as Repo>::Error

Source§

fn fetch<'life0, 'async_trait, D, S>( &'life0 self, id: D, session: S, ) -> Pin<Box<dyn Future<Output = Result<Option<D::Output>, Self::Error>> + 'async_trait>>
where D: 'async_trait + Dereference, S: 'async_trait + Session, Self: 'async_trait, 'life0: 'async_trait,

Source§

impl<'a, T> Repo for &'a mut T
where T: Repo,

Source§

type Error = <T as Repo>::Error

Source§

fn fetch<'life0, 'async_trait, D, S>( &'life0 self, id: D, session: S, ) -> Pin<Box<dyn Future<Output = Result<Option<D::Output>, Self::Error>> + 'async_trait>>
where D: 'async_trait + Dereference, S: 'async_trait + Session, Self: 'async_trait, 'life0: 'async_trait,

Source§

impl<T> Repo for Box<T>
where T: Repo,

Source§

type Error = <T as Repo>::Error

Source§

fn fetch<'life0, 'async_trait, D, S>( &'life0 self, id: D, session: S, ) -> Pin<Box<dyn Future<Output = Result<Option<D::Output>, Self::Error>> + 'async_trait>>
where D: 'async_trait + Dereference, S: 'async_trait + Session, Self: 'async_trait, 'life0: 'async_trait,

Source§

impl<T> Repo for Rc<T>
where T: Repo,

Source§

type Error = <T as Repo>::Error

Source§

fn fetch<'life0, 'async_trait, D, S>( &'life0 self, id: D, session: S, ) -> Pin<Box<dyn Future<Output = Result<Option<D::Output>, Self::Error>> + 'async_trait>>
where D: 'async_trait + Dereference, S: 'async_trait + Session, Self: 'async_trait, 'life0: 'async_trait,

Source§

impl<T> Repo for Arc<T>
where T: Repo,

Source§

type Error = <T as Repo>::Error

Source§

fn fetch<'life0, 'async_trait, D, S>( &'life0 self, id: D, session: S, ) -> Pin<Box<dyn Future<Output = Result<Option<D::Output>, Self::Error>> + 'async_trait>>
where D: 'async_trait + Dereference, S: 'async_trait + Session, Self: 'async_trait, 'life0: 'async_trait,

Implementors§