Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions library/core/src/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,7 @@
//! ## Examples
//!
//! ```
//! # #![allow(incomplete_features)]
//! # #![feature(provide_any)]
//! # #![feature(trait_upcasting)]
//! use std::any::{Provider, Demand, request_ref};
//!
//! // Definition of MyTrait
Expand All @@ -124,7 +122,7 @@
//! impl dyn MyTrait + '_ {
//! /// Common case: get a reference to a field of the error.
//! pub fn get_context_ref<T: ?Sized + 'static>(&self) -> Option<&T> {
//! request_ref::<T>(self)
//! request_ref(|demand| self.provide(demand))
//! }
//! }
//!
Expand Down Expand Up @@ -785,23 +783,32 @@ pub trait Provider {

/// Request a value from the `Provider`.
#[unstable(feature = "provide_any", issue = "none")]
pub fn request_value<'a, T: 'static>(provider: &'a dyn Provider) -> Option<T> {
request_by_type_tag::<'a, tags::Value<T>>(provider)
pub fn request_value<'a, T, F>(f: F) -> Option<T>
where
T: 'static,
F: FnOnce(&mut Demand<'a>),
{
request_by_type_tag::<'a, tags::Value<T>, _>(f)
}

/// Request a reference from the `Provider`.
#[unstable(feature = "provide_any", issue = "none")]
pub fn request_ref<'a, T: ?Sized + 'static>(provider: &'a dyn Provider) -> Option<&'a T> {
request_by_type_tag::<'a, tags::Ref<tags::MaybeSizedValue<T>>>(provider)
pub fn request_ref<'a, T, F>(f: F) -> Option<&'a T>
where
T: ?Sized + 'static,
F: FnOnce(&mut Demand<'a>),
{
request_by_type_tag::<'a, tags::Ref<tags::MaybeSizedValue<T>>, _>(f)
}

/// Request a specific value by tag from the `Provider`.
fn request_by_type_tag<'a, I>(provider: &'a dyn Provider) -> Option<I::Reified>
fn request_by_type_tag<'a, I, F>(f: F) -> Option<I::Reified>
where
I: tags::Type<'a>,
F: FnOnce(&mut Demand<'a>),
{
let mut tagged = TaggedOption::<'a, I>(None);
provider.provide(tagged.as_demand());
f(tagged.as_demand());
tagged.0
}

Expand Down
50 changes: 50 additions & 0 deletions src/test/ui/provider/provider_as_supertrait.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// run-pass
#![feature(provide_any)]
use std::any::{Provider, Demand, request_ref, request_value};

// Definition of MyTrait
trait MyTrait: Provider {
// ...
}

// Methods on `MyTrait` trait objects.
impl dyn MyTrait + '_ {
/// Common case: get a reference to a field of the error.
pub fn get_context_ref<T: ?Sized + 'static>(&self) -> Option<&T> {
request_ref(|demand| self.provide(demand))
}

/// Common case: get a reference to a field of the error.
pub fn get_context<T: 'static>(&self) -> Option<T> {
request_value(|demand| self.provide(demand))
}
}

struct IsFatal;

struct SomeFatalError;

impl MyTrait for SomeFatalError {}

impl Provider for SomeFatalError {
fn provide<'a>(&'a self, req: &mut Demand<'a>) {
req.provide_ref(&IsFatal).provide_value(|| IsFatal);
}
}

struct SomeNonFatalError;

impl MyTrait for SomeNonFatalError {}

impl Provider for SomeNonFatalError {
fn provide<'a>(&'a self, _req: &mut Demand<'a>) { }
}

fn is_fatal(obj: &dyn MyTrait) -> bool {
obj.get_context_ref::<IsFatal>().is_some() && obj.get_context::<IsFatal>().is_some()
}

fn main() {
assert!(is_fatal(&SomeFatalError));
assert!(!is_fatal(&SomeNonFatalError));
}