Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User defined types in Env #1910

Merged
merged 1 commit into from
Aug 16, 2021
Merged
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
28 changes: 23 additions & 5 deletions druid/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

//! An environment which is passed downward into the widget tree.

use std::any;
use std::any::{self, Any};
use std::borrow::Borrow;
use std::collections::{hash_map::Entry, HashMap};
use std::fmt::{Debug, Formatter};
Expand Down Expand Up @@ -92,11 +92,8 @@ pub struct Key<T> {
value_type: PhantomData<T>,
}

// we could do some serious deriving here: the set of types that can be stored
// could be defined per-app
// Also consider Box<Any> (though this would also impact debug).
/// A dynamic type representing all values that can be stored in an environment.
#[derive(Clone, Data, PartialEq)]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This removes PartialEq. it is not straight forward to implement with Arc<dyn Any>. I think Data should be sufficient

#[derive(Clone, Data)]
#[allow(missing_docs)]
// ANCHOR: value_type
pub enum Value {
Expand All @@ -110,6 +107,7 @@ pub enum Value {
UnsignedInt(u64),
String(ArcStr),
Font(FontDescriptor),
Other(Arc<dyn Any + Send + Sync>),
}
// ANCHOR_END: value_type

Expand Down Expand Up @@ -472,6 +470,7 @@ impl Debug for Value {
Value::UnsignedInt(x) => write!(f, "UnsignedInt {}", x),
Value::String(s) => write!(f, "String {:?}", s),
Value::Font(font) => write!(f, "Font {:?}", font),
Value::Other(other) => write!(f, "{:?}", other),
}
}
}
Expand Down Expand Up @@ -615,6 +614,25 @@ impl_value_type!(Insets, Insets);
impl_value_type!(ArcStr, String);
impl_value_type!(FontDescriptor, Font);

impl<T: 'static + Send + Sync> From<Arc<T>> for Value {
fn from(this: Arc<T>) -> Value {
Value::Other(this)
}
}

impl<T: 'static + Send + Sync> ValueType for Arc<T> {
fn try_from_value(v: &Value) -> Result<Self, ValueTypeError> {
let err = ValueTypeError {
expected: any::type_name::<T>(),
found: v.clone(),
};
match v {
Value::Other(o) => o.clone().downcast::<T>().map_err(|_| err),
_ => Err(err),
}
}
}

impl<T: ValueType> KeyOrValue<T> {
/// Resolve the concrete type `T` from this `KeyOrValue`, using the provided
/// [`Env`] if required.
Expand Down