@@ -796,7 +796,7 @@ pub trait Provider {
796
796
/// impl Provider for SomeConcreteType {
797
797
/// fn provide<'a>(&'a self, demand: &mut Demand<'a>) {
798
798
/// demand.provide_ref::<str>(&self.field)
799
- /// .provide_value::<i32>(|| self.num_field);
799
+ /// .provide_value::<i32>(self.num_field);
800
800
/// }
801
801
/// }
802
802
/// ```
@@ -881,36 +881,64 @@ impl<'a> Demand<'a> {
881
881
///
882
882
/// # Examples
883
883
///
884
+ /// Provides an `u8`.
885
+ ///
886
+ /// ```rust
887
+ /// #![feature(provide_any)]
888
+ ///
889
+ /// use std::any::{Provider, Demand};
890
+ /// # struct SomeConcreteType { field: u8 }
891
+ ///
892
+ /// impl Provider for SomeConcreteType {
893
+ /// fn provide<'a>(&'a self, demand: &mut Demand<'a>) {
894
+ /// demand.provide_value::<u8>(self.field);
895
+ /// }
896
+ /// }
897
+ /// ```
898
+ #[ unstable( feature = "provide_any" , issue = "96024" ) ]
899
+ pub fn provide_value < T > ( & mut self , value : T ) -> & mut Self
900
+ where
901
+ T : ' static ,
902
+ {
903
+ self . provide :: < tags:: Value < T > > ( value)
904
+ }
905
+
906
+ /// Provide a value or other type with only static lifetimes computed using a closure.
907
+ ///
908
+ /// # Examples
909
+ ///
884
910
/// Provides a `String` by cloning.
885
911
///
886
912
/// ```rust
887
- /// # #![feature(provide_any)]
913
+ /// #![feature(provide_any)]
914
+ ///
888
915
/// use std::any::{Provider, Demand};
889
916
/// # struct SomeConcreteType { field: String }
890
917
///
891
918
/// impl Provider for SomeConcreteType {
892
919
/// fn provide<'a>(&'a self, demand: &mut Demand<'a>) {
893
- /// demand.provide_value ::<String>(|| self.field.clone());
920
+ /// demand.provide_value_with ::<String>(|| self.field.clone());
894
921
/// }
895
922
/// }
896
923
/// ```
897
924
#[ unstable( feature = "provide_any" , issue = "96024" ) ]
898
- pub fn provide_value < T > ( & mut self , fulfil : impl FnOnce ( ) -> T ) -> & mut Self
925
+ pub fn provide_value_with < T > ( & mut self , fulfil : impl FnOnce ( ) -> T ) -> & mut Self
899
926
where
900
927
T : ' static ,
901
928
{
902
929
self . provide_with :: < tags:: Value < T > > ( fulfil)
903
930
}
904
931
905
- /// Provide a reference, note that the referee type must be bounded by `'static`,
932
+ /// Provide a reference. The referee type must be bounded by `'static`,
906
933
/// but may be unsized.
907
934
///
908
935
/// # Examples
909
936
///
910
937
/// Provides a reference to a field as a `&str`.
911
938
///
912
939
/// ```rust
913
- /// # #![feature(provide_any)]
940
+ /// #![feature(provide_any)]
941
+ ///
914
942
/// use std::any::{Provider, Demand};
915
943
/// # struct SomeConcreteType { field: String }
916
944
///
@@ -925,6 +953,40 @@ impl<'a> Demand<'a> {
925
953
self . provide :: < tags:: Ref < tags:: MaybeSizedValue < T > > > ( value)
926
954
}
927
955
956
+ /// Provide a reference computed using a closure. The referee type
957
+ /// must be bounded by `'static`, but may be unsized.
958
+ ///
959
+ /// # Examples
960
+ ///
961
+ /// Provides a reference to a field as a `&str`.
962
+ ///
963
+ /// ```rust
964
+ /// #![feature(provide_any)]
965
+ ///
966
+ /// use std::any::{Provider, Demand};
967
+ /// # struct SomeConcreteType { business: String, party: String }
968
+ /// # fn today_is_a_weekday() -> bool { true }
969
+ ///
970
+ /// impl Provider for SomeConcreteType {
971
+ /// fn provide<'a>(&'a self, demand: &mut Demand<'a>) {
972
+ /// demand.provide_ref_with::<str>(|| {
973
+ /// if today_is_a_weekday() {
974
+ /// &self.business
975
+ /// } else {
976
+ /// &self.party
977
+ /// }
978
+ /// });
979
+ /// }
980
+ /// }
981
+ /// ```
982
+ #[ unstable( feature = "provide_any" , issue = "96024" ) ]
983
+ pub fn provide_ref_with < T : ?Sized + ' static > (
984
+ & mut self ,
985
+ fulfil : impl FnOnce ( ) -> & ' a T ,
986
+ ) -> & mut Self {
987
+ self . provide_with :: < tags:: Ref < tags:: MaybeSizedValue < T > > > ( fulfil)
988
+ }
989
+
928
990
/// Provide a value with the given `Type` tag.
929
991
fn provide < I > ( & mut self , value : I :: Reified ) -> & mut Self
930
992
where
0 commit comments