From 0c72be3e1a6da6f6fc9bab0a63edf780de7d985d Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Mon, 13 Jun 2022 09:29:55 +0100 Subject: [PATCH] core::any: replace some unstable generic types with impl Trait Signed-off-by: Nick Cameron --- library/core/src/any.rs | 35 +++++++++++++++-------------------- library/core/tests/any.rs | 22 +++++++++++----------- 2 files changed, 26 insertions(+), 31 deletions(-) diff --git a/library/core/src/any.rs b/library/core/src/any.rs index 866419ac34b11..f20c497a183b2 100644 --- a/library/core/src/any.rs +++ b/library/core/src/any.rs @@ -125,7 +125,7 @@ //! impl dyn MyTrait + '_ { //! /// Get a reference to a field of the implementing struct. //! pub fn get_context_by_ref(&self) -> Option<&T> { -//! request_ref::(self) +//! request_ref::(self) //! } //! } //! @@ -799,7 +799,7 @@ pub trait Provider { /// impl Provider for SomeConcreteType { /// fn provide<'a>(&'a self, demand: &mut Demand<'a>) { /// demand.provide_ref::(&self.field) - /// .provide_value::(|| self.num_field); + /// .provide_value::(|| self.num_field); /// } /// } /// ``` @@ -817,17 +817,16 @@ pub trait Provider { /// # #![feature(provide_any)] /// use std::any::{Provider, request_value}; /// -/// fn get_string(provider: &P) -> String { -/// request_value::(provider).unwrap() +/// fn get_string(provider: &impl Provider) -> String { +/// request_value::(provider).unwrap() /// } /// ``` #[unstable(feature = "provide_any", issue = "96024")] -pub fn request_value<'a, T, P>(provider: &'a P) -> Option +pub fn request_value<'a, T>(provider: &'a (impl Provider + ?Sized)) -> Option where T: 'static, - P: Provider + ?Sized, { - request_by_type_tag::<'a, tags::Value, P>(provider) + request_by_type_tag::<'a, tags::Value>(provider) } /// Request a reference from the `Provider`. @@ -840,24 +839,22 @@ where /// # #![feature(provide_any)] /// use std::any::{Provider, request_ref}; /// -/// fn get_str(provider: &P) -> &str { -/// request_ref::(provider).unwrap() +/// fn get_str(provider: &impl Provider) -> &str { +/// request_ref::(provider).unwrap() /// } /// ``` #[unstable(feature = "provide_any", issue = "96024")] -pub fn request_ref<'a, T, P>(provider: &'a P) -> Option<&'a T> +pub fn request_ref<'a, T>(provider: &'a (impl Provider + ?Sized)) -> Option<&'a T> where T: 'static + ?Sized, - P: Provider + ?Sized, { - request_by_type_tag::<'a, tags::Ref>, P>(provider) + request_by_type_tag::<'a, tags::Ref>>(provider) } /// Request a specific value by tag from the `Provider`. -fn request_by_type_tag<'a, I, P>(provider: &'a P) -> Option +fn request_by_type_tag<'a, I>(provider: &'a (impl Provider + ?Sized)) -> Option where I: tags::Type<'a>, - P: Provider + ?Sized, { let mut tagged = TaggedOption::<'a, I>(None); provider.provide(tagged.as_demand()); @@ -896,17 +893,16 @@ impl<'a> Demand<'a> { /// /// impl Provider for SomeConcreteType { /// fn provide<'a>(&'a self, demand: &mut Demand<'a>) { - /// demand.provide_value::(|| self.field.clone()); + /// demand.provide_value::(|| self.field.clone()); /// } /// } /// ``` #[unstable(feature = "provide_any", issue = "96024")] - pub fn provide_value(&mut self, fulfil: F) -> &mut Self + pub fn provide_value(&mut self, fulfil: impl FnOnce() -> T) -> &mut Self where T: 'static, - F: FnOnce() -> T, { - self.provide_with::, F>(fulfil) + self.provide_with::>(fulfil) } /// Provide a reference, note that the referee type must be bounded by `'static`, @@ -944,10 +940,9 @@ impl<'a> Demand<'a> { } /// Provide a value with the given `Type` tag, using a closure to prevent unnecessary work. - fn provide_with(&mut self, fulfil: F) -> &mut Self + fn provide_with(&mut self, fulfil: impl FnOnce() -> I::Reified) -> &mut Self where I: tags::Type<'a>, - F: FnOnce() -> I::Reified, { if let Some(res @ TaggedOption(None)) = self.0.downcast_mut::() { res.0 = Some(fulfil()); diff --git a/library/core/tests/any.rs b/library/core/tests/any.rs index cdc6fadbab707..8ed0c88808fe2 100644 --- a/library/core/tests/any.rs +++ b/library/core/tests/any.rs @@ -142,7 +142,7 @@ impl Provider for SomeConcreteType { demand .provide_ref::(&self.some_string) .provide_ref::(&self.some_string) - .provide_value::(|| "bye".to_owned()); + .provide_value::(|| "bye".to_owned()); } } @@ -151,9 +151,9 @@ impl Provider for SomeConcreteType { fn test_provider() { let obj: &dyn Provider = &SomeConcreteType { some_string: "hello".to_owned() }; - assert_eq!(&**request_ref::(obj).unwrap(), "hello"); - assert_eq!(&*request_value::(obj).unwrap(), "bye"); - assert_eq!(request_value::(obj), None); + assert_eq!(&**request_ref::(obj).unwrap(), "hello"); + assert_eq!(&*request_value::(obj).unwrap(), "bye"); + assert_eq!(request_value::(obj), None); } // Test the provide and request mechanisms with a boxed trait object. @@ -161,9 +161,9 @@ fn test_provider() { fn test_provider_boxed() { let obj: Box = Box::new(SomeConcreteType { some_string: "hello".to_owned() }); - assert_eq!(&**request_ref::(&*obj).unwrap(), "hello"); - assert_eq!(&*request_value::(&*obj).unwrap(), "bye"); - assert_eq!(request_value::(&*obj), None); + assert_eq!(&**request_ref::(&*obj).unwrap(), "hello"); + assert_eq!(&*request_value::(&*obj).unwrap(), "bye"); + assert_eq!(request_value::(&*obj), None); } // Test the provide and request mechanisms with a concrete object. @@ -171,9 +171,9 @@ fn test_provider_boxed() { fn test_provider_concrete() { let obj = SomeConcreteType { some_string: "hello".to_owned() }; - assert_eq!(&**request_ref::(&obj).unwrap(), "hello"); - assert_eq!(&*request_value::(&obj).unwrap(), "bye"); - assert_eq!(request_value::(&obj), None); + assert_eq!(&**request_ref::(&obj).unwrap(), "hello"); + assert_eq!(&*request_value::(&obj).unwrap(), "bye"); + assert_eq!(request_value::(&obj), None); } trait OtherTrait: Provider {} @@ -182,7 +182,7 @@ impl OtherTrait for SomeConcreteType {} impl dyn OtherTrait { fn get_ref(&self) -> Option<&T> { - request_ref::(self) + request_ref::(self) } }