1#![doc = include_str!("../README.md")]
14#![no_std]
15#![warn(missing_docs)]
16#![cfg_attr(docsrs, feature(doc_auto_cfg))]
17
18#[cfg(any(feature = "alloc", doc))]
19extern crate alloc;
20
21#[cfg(feature = "alloc")]
22mod feature_alloc;
23#[doc(hidden)]
24pub mod lt;
25mod provide;
26mod query;
27mod request_macro;
28mod tag;
29mod tag_macro;
30#[doc(hidden)]
31pub mod type_fn;
32
33pub use lt::{LifetimeHkt, Lt};
34pub use provide::{
35    for_each_provided_tag_id, get_provided_tag_ids, provide_by_ref_with, provide_with,
36    when_provider, Provide, ProvideRef, WhenProvider,
37};
38#[doc(inline)]
39pub use query::{Query, QueryUsing};
40pub use tag::{Mut, Ref, ResourceTag, TagFor, TagId, Value};
41pub use type_fn::TypeFn;
42
43#[cfg(feature = "alloc")]
44pub use feature_alloc::ProvideBox;
45
46#[doc(hidden)]
48pub mod __m {
49    pub use core::{
50        convert::Infallible,
51        marker::PhantomData,
52        result::Result::{self, Err, Ok},
53    };
54
55    use crate::{tag::TagFor, Lt, Mut, Provide, Ref, Value};
56
57    pub struct RequestHelper<L, P, Out> {
58        state: Result<Out, P>,
59        _l: PhantomData<L>,
60    }
61
62    impl<L, P, Out> RequestHelper<L, P, Out>
63    where
64        L: Lt,
65        P: Provide<L>,
66    {
67        pub fn new(provider: P) -> Self {
68            Self {
69                state: Err(provider),
70                _l: PhantomData,
71            }
72        }
73
74        pub fn from_out(out: Out) -> Self {
75            Self {
76                state: Ok(out),
77                _l: PhantomData,
78            }
79        }
80
81        pub fn request<Tag: TagFor<L>>(self, arg: Tag::ArgValue) -> Result<Tag::Value, Self> {
82            let Err(p) = self.state else {
83                return Err(self);
84            };
85            p.request::<Tag>(arg).map_err(Self::new)
86        }
87
88        pub fn request_value<T: 'static>(
89            self,
90            arg: <Value<T> as TagFor<L>>::ArgValue,
91        ) -> Result<T, Self>
92        where
93            Value<T>: TagFor<L, Value = T>,
94        {
95            self.request::<Value<T>>(arg)
96        }
97
98        pub fn finish(self) -> Result<Out, P> {
99            self.state
100        }
101    }
102
103    impl<'x, L, P, Out> RequestHelper<Lt!['x, ..L], P, Out>
104    where
105        L: Lt,
106        P: Provide<Lt!['x, ..L]>,
107    {
108        pub fn request_ref<T: 'static + ?Sized>(self, arg: ()) -> Result<&'x T, Self> {
109            self.request::<Ref<Value<T>>>(arg)
110        }
111
112        pub fn request_mut<T: 'static + ?Sized>(self, arg: ()) -> Result<&'x mut T, Self> {
113            self.request::<Mut<Value<T>>>(arg)
114        }
115    }
116}