pdk_classy/extract/
from_context_once.rs1use std::{
3 future::{ready, Future, Ready},
4 ops::Deref,
5};
6
7use super::FromContext;
8use crate::BoxError;
9
10mod mode {
11 pub enum Once {}
12
13 pub enum Repeat {}
14}
15
16pub struct Exclusive<'c, C> {
17 context: &'c C,
18}
19
20impl<'c, C> Exclusive<'c, C> {
21 pub(crate) fn new(context: &'c C) -> Self {
22 Self { context }
23 }
24
25 pub fn get(&self) -> &'c C {
26 self.context
27 }
28}
29
30impl<C> Deref for Exclusive<'_, C> {
31 type Target = C;
32
33 fn deref(&self) -> &Self::Target {
34 self.get()
35 }
36}
37
38pub trait FromContextOnce<C, M = mode::Once>: Sized {
39 type Error: Into<BoxError>;
40
41 type Future<'c>: Future<Output = Result<Self, Self::Error>> + 'c
42 where
43 C: 'c;
44
45 fn from_context_once(context: Exclusive<C>) -> Self::Future<'_>;
46}
47
48impl<C, T> FromContextOnce<C, mode::Repeat> for T
49where
50 for<'c> T: FromContext<C> + 'c,
51{
52 type Error = T::Error;
53
54 type Future<'c>
55 = Ready<Result<Self, Self::Error>>
56 where
57 C: 'c,
58 Self: 'c;
59
60 fn from_context_once(context: Exclusive<C>) -> Self::Future<'_> {
61 ready(T::from_context(&context))
62 }
63}