Skip to main content

topcoat_core/
internal.rs

1/// Converts a borrowed memoized `Option` or `Result` into the ergonomic
2/// borrowed shape exposed by `#[memoize]`.
3///
4/// `#[memoize]` stores the function's original return value in the request
5/// cache and normally returns `&T`. For top-level `Option<T>` and
6/// `Result<T, E>` return values, the macro applies `.as_ref()` and publishes
7/// this associated type instead, yielding `Option<&T>` and `Result<&T, &E>`.
8#[doc(hidden)]
9pub trait MemoizeAsRef {
10    /// The return type produced after borrowing the cached value's contents.
11    type AsRef;
12}
13
14impl<'a, T> MemoizeAsRef for &'a Option<T> {
15    type AsRef = Option<&'a T>;
16}
17
18impl<'a, T, E> MemoizeAsRef for &'a Result<T, E> {
19    type AsRef = Result<&'a T, &'a E>;
20}
21
22pub trait ResultExt {
23    type T;
24    type E;
25}
26
27impl<T, E> ResultExt for Result<T, E> {
28    type T = T;
29    type E = E;
30}