Add From<Vec<T>> and From<HashMap<String, T>> for Dynamic.
This commit is contained in:
parent
c69647d9fd
commit
c40c0a0bc3
17
README.md
17
README.md
@ -535,6 +535,16 @@ match item.type_name() { // 'type_name' returns the name
|
||||
}
|
||||
```
|
||||
|
||||
The following conversion traits are implemented for `Dynamic`:
|
||||
|
||||
* `From<i64>` (`i32` if [`only_i32`])
|
||||
* `From<f64>` (if not [`no_float`])
|
||||
* `From<bool>`
|
||||
* `From<String>`
|
||||
* `From<char>`
|
||||
* `From<Vec<T>>` (into an [array])
|
||||
* `From<HashMap<String, T>>` (into an [object map]).
|
||||
|
||||
Value conversions
|
||||
-----------------
|
||||
|
||||
@ -1430,6 +1440,7 @@ engine.register_fn("push", |list: &mut Array, item: MyType| list.push(Box::new(i
|
||||
Object maps
|
||||
-----------
|
||||
|
||||
[`Map`]: #object-maps
|
||||
[object map]: #object-maps
|
||||
[object maps]: #object-maps
|
||||
|
||||
@ -1631,12 +1642,12 @@ Boolean operators
|
||||
-----------------
|
||||
|
||||
| Operator | Description |
|
||||
| -------- | ------------------------------- |
|
||||
| -------- | ------------------------------------- |
|
||||
| `!` | Boolean _Not_ |
|
||||
| `&&` | Boolean _And_ (short-circuits) |
|
||||
| `\|\|` | Boolean _Or_ (short-circuits) |
|
||||
| `&` | Boolean _And_ (full evaluation) |
|
||||
| `\|` | Boolean _Or_ (full evaluation) |
|
||||
| `&` | Boolean _And_ (doesn't short-circuit) |
|
||||
| `\|` | Boolean _Or_ (doesn't short-circuit) |
|
||||
|
||||
Double boolean operators `&&` and `||` _short-circuit_, meaning that the second operand will not be evaluated
|
||||
if the first one already proves the condition wrong.
|
||||
|
21
src/any.rs
21
src/any.rs
@ -9,6 +9,7 @@ use crate::parser::FLOAT;
|
||||
use crate::stdlib::{
|
||||
any::{type_name, Any, TypeId},
|
||||
boxed::Box,
|
||||
collections::HashMap,
|
||||
fmt,
|
||||
string::String,
|
||||
};
|
||||
@ -65,6 +66,9 @@ impl<T: Any + Clone> Variant for T {
|
||||
}
|
||||
|
||||
/// A trait to represent any type.
|
||||
///
|
||||
/// `From<_>` is implemented for `i64` (`i32` if `only_i32`), `f64` (if not `no_float`),
|
||||
/// `bool`, `String`, `char`, `Vec<T>` (into `Array`) and `HashMap<String, T>` (into `Map`).
|
||||
#[cfg(feature = "sync")]
|
||||
pub trait Variant: Any + Send + Sync {
|
||||
/// Convert this `Variant` trait object to `&dyn Any`.
|
||||
@ -525,6 +529,23 @@ impl From<String> for Dynamic {
|
||||
Self(Union::Str(Box::new(value)))
|
||||
}
|
||||
}
|
||||
impl<T: Variant + Clone> From<Vec<T>> for Dynamic {
|
||||
fn from(value: Vec<T>) -> Self {
|
||||
Self(Union::Array(Box::new(
|
||||
value.into_iter().map(Dynamic::from).collect(),
|
||||
)))
|
||||
}
|
||||
}
|
||||
impl<T: Variant + Clone> From<HashMap<String, T>> for Dynamic {
|
||||
fn from(value: HashMap<String, T>) -> Self {
|
||||
Self(Union::Map(Box::new(
|
||||
value
|
||||
.into_iter()
|
||||
.map(|(k, v)| (k, Dynamic::from(v)))
|
||||
.collect(),
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
/// Private type which ensures that `rhai::Any` and `rhai::AnyExt` can only
|
||||
/// be implemented by this crate.
|
||||
|
Loading…
Reference in New Issue
Block a user