topcoat-core 0.10.0

A modular, batteries-included Rust web framework for server-rendered apps.
Documentation
/// The borrowed return type of a `#[memoize(as_ref)]` function.
///
/// `#[memoize]` normally returns `&T` from the request cache. The `as_ref`
/// option uses this trait to borrow the cached value's contents instead.
/// For example, `Option<T>` becomes `Option<&T>` and `Result<T, E>` becomes
/// `Result<&T, &E>`.
///
/// Implement this trait for your own return type to use it with
/// `#[memoize(as_ref)]`:
///
/// ```rust
/// use topcoat::context::MemoizeAsRef;
///
/// /// A custom optional value.
/// enum Maybe<T> {
///     Some(T),
///     None,
/// }
///
/// impl<T> MemoizeAsRef for Maybe<T> {
///     type AsRef<'a>
///         = Maybe<&'a T>
///     where
///         Self: 'a;
///
///     fn as_ref(&self) -> Self::AsRef<'_> {
///         match self {
///             Maybe::Some(value) => Maybe::Some(value),
///             Maybe::None => Maybe::None,
///         }
///     }
/// }
/// ```
pub trait MemoizeAsRef {
    /// The return type produced by borrowing the cached value's contents.
    type AsRef<'a>
    where
        Self: 'a;

    /// Borrows the cached value's contents.
    fn as_ref(&self) -> Self::AsRef<'_>;
}

impl<T> MemoizeAsRef for Option<T> {
    type AsRef<'a>
        = Option<&'a T>
    where
        Self: 'a;

    fn as_ref(&self) -> Self::AsRef<'_> {
        Option::as_ref(self)
    }
}

impl<T, E> MemoizeAsRef for Result<T, E> {
    type AsRef<'a>
        = Result<&'a T, &'a E>
    where
        Self: 'a;

    fn as_ref(&self) -> Self::AsRef<'_> {
        Result::as_ref(self)
    }
}