Skip to content

Commit 17730e6

Browse files
committed
Update docs
Signed-off-by: Nick Cameron <[email protected]>
1 parent 57d027d commit 17730e6

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

library/core/src/any.rs

+22-19
Original file line numberDiff line numberDiff line change
@@ -91,20 +91,21 @@
9191
//! `Provider` and the associated APIs support generic, type-driven access to data, and a mechanism
9292
//! for implementers to provide such data. The key parts of the interface are the `Provider`
9393
//! 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
9595
//! should not call `request_*` directly, they are helper functions for intermediate implementers
9696
//! to use to implement a user-facing interface.
9797
//!
9898
//! 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.
100100
//!
101101
//! ## Data flow
102102
//!
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`
104105
//! * `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
106107
//! 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.
108109
//! * `request_*` unpacks the `Demand` object and returns any stored value to the user.
109110
//!
110111
//! ## Examples
@@ -113,15 +114,15 @@
113114
//! # #![feature(provide_any)]
114115
//! use std::any::{Provider, Demand, request_ref};
115116
//!
116-
//! // Definition of MyTrait
117+
//! // Definition of MyTrait, a data provider.
117118
//! trait MyTrait: Provider {
118119
//! // ...
119120
//! }
120121
//!
121122
//! // Methods on `MyTrait` trait objects.
122123
//! 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> {
125126
//! request_ref::<T, _>(self)
126127
//! }
127128
//! }
@@ -134,19 +135,21 @@
134135
//!
135136
//! impl Provider for SomeConcreteType {
136137
//! fn provide<'a>(&'a self, req: &mut Demand<'a>) {
138+
//! // Provide a string reference. We could provide multiple values with
139+
//! // different types here.
137140
//! req.provide_ref::<String>(&self.some_string);
138141
//! }
139142
//! }
140143
//!
141144
//! // Downstream usage of `MyTrait`.
142145
//! fn use_my_trait(obj: &dyn MyTrait) {
143146
//! // Request a &String from obj.
144-
//! let _ = obj.get_context_ref::<String>().unwrap();
147+
//! let _ = obj.get_context_by_ref::<String>().unwrap();
145148
//! }
146149
//! ```
147150
//!
148151
//! 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`.
150153
151154
#![stable(feature = "rust1", since = "1.0.0")]
152155

@@ -775,10 +778,10 @@ pub const fn type_name_of_val<T: ?Sized>(_val: &T) -> &'static str {
775778
/// Trait implemented by a type which can dynamically provide values based on type.
776779
#[unstable(feature = "provide_any", issue = "none")]
777780
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`.
780783
#[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>);
782785
}
783786

784787
/// Request a value from the `Provider`.
@@ -816,12 +819,11 @@ where
816819
// Demand and its methods
817820
///////////////////////////////////////////////////////////////////////////////
818821

819-
/// A helper object for providing objects by type.
822+
/// A helper object for providing data by type.
820823
///
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.
822825
#[allow(missing_debug_implementations)]
823826
#[unstable(feature = "provide_any", issue = "none")]
824-
// SAFETY: `TaggedOption::as_demand` relies on this precise definition.
825827
#[repr(transparent)]
826828
pub struct Demand<'a>(dyn Erased<'a> + 'a);
827829

@@ -836,7 +838,8 @@ impl<'a> Demand<'a> {
836838
self.provide_with::<tags::Value<T>, F>(fulfil)
837839
}
838840

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.
840843
#[unstable(feature = "provide_any", issue = "none")]
841844
pub fn provide_ref<T: ?Sized + 'static>(&mut self, value: &'a T) -> &mut Self {
842845
self.provide::<tags::Ref<tags::MaybeSizedValue<T>>>(value)
@@ -902,7 +905,7 @@ mod tags {
902905
type Reified = T::Reified;
903906
}
904907

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.
906909
#[derive(Debug)]
907910
pub struct Value<T: 'static>(PhantomData<T>);
908911

@@ -960,7 +963,7 @@ impl<'a, I: tags::Type<'a>> Erased<'a> for TaggedOption<'a, I> {
960963
#[unstable(feature = "provide_any", issue = "none")]
961964
impl<'a> dyn Erased<'a> {
962965
/// Returns some reference to the dynamic value if it is tagged with `I`,
963-
/// or `None` if it isn't.
966+
/// or `None` otherwise.
964967
#[inline]
965968
fn downcast_mut<I>(&mut self) -> Option<&mut TaggedOption<'a, I>>
966969
where

0 commit comments

Comments
 (0)