91
91
//! `Provider` and the associated APIs support generic, type-driven access to data, and a mechanism
92
92
//! for implementers to provide such data. The key parts of the interface are the `Provider`
93
93
//! trait for objects which can provide data, and the [`request_value`] and [`request_ref`]
94
- //! functions for requesting data from an object which implements `Provider`. Note that end users
94
+ //! functions for requesting data from an object which implements `Provider`. Generally, end users
95
95
//! should not call `request_*` directly, they are helper functions for intermediate implementers
96
96
//! to use to implement a user-facing interface.
97
97
//!
98
98
//! Typically, a data provider is a trait object of a trait which extends `Provider`. A user will
99
- //! request data from the trait object by specifying the type.
99
+ //! request data from a trait object by specifying the type of the data .
100
100
//!
101
101
//! ## Data flow
102
102
//!
103
- //! * A user requests an object, which is delegated to `request_value` or `request_ref`
103
+ //! * A user requests an object of a specific type, which is delegated to `request_value` or
104
+ //! `request_ref`
104
105
//! * `request_*` creates a `Demand` object and passes it to `Provider::provide`
105
- //! * The object provider's implementation of `Provider::provide` tries providing values of
106
+ //! * The data provider's implementation of `Provider::provide` tries providing values of
106
107
//! different types using `Demand::provide_*`. If the type matches the type requested by
107
- //! the user, it will be stored in the `Demand` object.
108
+ //! the user, the value will be stored in the `Demand` object.
108
109
//! * `request_*` unpacks the `Demand` object and returns any stored value to the user.
109
110
//!
110
111
//! ## Examples
113
114
//! # #![feature(provide_any)]
114
115
//! use std::any::{Provider, Demand, request_ref};
115
116
//!
116
- //! // Definition of MyTrait
117
+ //! // Definition of MyTrait, a data provider.
117
118
//! trait MyTrait: Provider {
118
119
//! // ...
119
120
//! }
120
121
//!
121
122
//! // Methods on `MyTrait` trait objects.
122
123
//! impl dyn MyTrait + '_ {
123
- //! /// Common case: get a reference to a field of the struct.
124
- //! pub fn get_context_ref <T: ?Sized + 'static>(&self) -> Option<&T> {
124
+ //! /// Get a reference to a field of the implementing struct.
125
+ //! pub fn get_context_by_ref <T: ?Sized + 'static>(&self) -> Option<&T> {
125
126
//! request_ref::<T, _>(self)
126
127
//! }
127
128
//! }
134
135
//!
135
136
//! impl Provider for SomeConcreteType {
136
137
//! fn provide<'a>(&'a self, req: &mut Demand<'a>) {
138
+ //! // Provide a string reference. We could provide multiple values with
139
+ //! // different types here.
137
140
//! req.provide_ref::<String>(&self.some_string);
138
141
//! }
139
142
//! }
140
143
//!
141
144
//! // Downstream usage of `MyTrait`.
142
145
//! fn use_my_trait(obj: &dyn MyTrait) {
143
146
//! // Request a &String from obj.
144
- //! let _ = obj.get_context_ref ::<String>().unwrap();
147
+ //! let _ = obj.get_context_by_ref ::<String>().unwrap();
145
148
//! }
146
149
//! ```
147
150
//!
148
151
//! In this example, if the concrete type of `obj` in `use_my_trait` is `SomeConcreteType`, then
149
- //! the `get_context_ref` call will return a reference to `obj.some_string`.
152
+ //! the `get_context_ref` call will return a reference to `obj.some_string` with type `&String` .
150
153
151
154
#![ stable( feature = "rust1" , since = "1.0.0" ) ]
152
155
@@ -775,10 +778,10 @@ pub const fn type_name_of_val<T: ?Sized>(_val: &T) -> &'static str {
775
778
/// Trait implemented by a type which can dynamically provide values based on type.
776
779
#[ unstable( feature = "provide_any" , issue = "none" ) ]
777
780
pub trait Provider {
778
- /// Object providers should implement this method to provide *all* values they are able to
779
- /// provide using `req `.
781
+ /// Data providers should implement this method to provide *all* values they are able to
782
+ /// provide by using `demand `.
780
783
#[ unstable( feature = "provide_any" , issue = "none" ) ]
781
- fn provide < ' a > ( & ' a self , req : & mut Demand < ' a > ) ;
784
+ fn provide < ' a > ( & ' a self , demand : & mut Demand < ' a > ) ;
782
785
}
783
786
784
787
/// Request a value from the `Provider`.
@@ -816,12 +819,11 @@ where
816
819
// Demand and its methods
817
820
///////////////////////////////////////////////////////////////////////////////
818
821
819
- /// A helper object for providing objects by type.
822
+ /// A helper object for providing data by type.
820
823
///
821
- /// An object provider provides values by calling this type's provide methods.
824
+ /// A data provider provides values by calling this type's provide methods.
822
825
#[ allow( missing_debug_implementations) ]
823
826
#[ unstable( feature = "provide_any" , issue = "none" ) ]
824
- // SAFETY: `TaggedOption::as_demand` relies on this precise definition.
825
827
#[ repr( transparent) ]
826
828
pub struct Demand < ' a > ( dyn Erased < ' a > + ' a ) ;
827
829
@@ -836,7 +838,8 @@ impl<'a> Demand<'a> {
836
838
self . provide_with :: < tags:: Value < T > , F > ( fulfil)
837
839
}
838
840
839
- /// Provide a reference, note that the referee type must be bounded by `'static`, but may be unsized.
841
+ /// Provide a reference, note that the referee type must be bounded by `'static`,
842
+ /// but may be unsized.
840
843
#[ unstable( feature = "provide_any" , issue = "none" ) ]
841
844
pub fn provide_ref < T : ?Sized + ' static > ( & mut self , value : & ' a T ) -> & mut Self {
842
845
self . provide :: < tags:: Ref < tags:: MaybeSizedValue < T > > > ( value)
@@ -902,7 +905,7 @@ mod tags {
902
905
type Reified = T :: Reified ;
903
906
}
904
907
905
- /// Type-based tag for types bounded by `'static`, i.e., with no borrowed element .
908
+ /// Type-based tag for types bounded by `'static`, i.e., with no borrowed elements .
906
909
#[ derive( Debug ) ]
907
910
pub struct Value < T : ' static > ( PhantomData < T > ) ;
908
911
@@ -960,7 +963,7 @@ impl<'a, I: tags::Type<'a>> Erased<'a> for TaggedOption<'a, I> {
960
963
#[ unstable( feature = "provide_any" , issue = "none" ) ]
961
964
impl < ' a > dyn Erased < ' a > {
962
965
/// Returns some reference to the dynamic value if it is tagged with `I`,
963
- /// or `None` if it isn't .
966
+ /// or `None` otherwise .
964
967
#[ inline]
965
968
fn downcast_mut < I > ( & mut self ) -> Option < & mut TaggedOption < ' a , I > >
966
969
where
0 commit comments