Skip to content

Commit

Permalink
Implement ToVariant and FromVariant for Variant
Browse files Browse the repository at this point in the history
This is useful when combined with the container variant functions and
trait implementations from gtk-rs#651.

BREAKING CHANGE: This remove `impl<T: ToVariant> From<T> for Variant`,
which made it impossible to implement `ToVariant` since it conflicts
with the standard library `impl<T> From<T> for T`:
gtk-rs#678
  • Loading branch information
ids1024 committed Sep 4, 2020
1 parent a8b5db0 commit 633b61a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 27 deletions.
46 changes: 25 additions & 21 deletions src/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,8 @@
//! assert_eq!(num.get::<i32>(), Some(10));
//! assert_eq!(num.get::<u32>(), None);
//!
//! // `Variant` implements `From`
//! let hello = Variant::from("Hello!");
//!
//! // `get_str` tries to borrow a string slice.
//! let hello = "Hello!".to_variant();
//! assert_eq!(hello.get_str(), Some("Hello!"));
//! assert_eq!(num.get_str(), None);
//!
Expand All @@ -44,7 +42,7 @@
//! assert_eq!(variant.get_str(), Some("Hello!"));
//!
//! // Variant carrying an array
//! let array = [Variant::from("Hello"), Variant::from("there!")];
//! let array = ["Hello".to_variant(), "there!".to_variant()];
//! let variant = Variant::new_array::<&str>(&array);
//! assert_eq!(variant.n_children(), 2);
//! assert_eq!(variant.get_child_value(0).get_str(), Some("Hello"));
Expand Down Expand Up @@ -523,12 +521,6 @@ impl ToVariant for str {
}
}

impl<T: ToVariant> From<T> for Variant {
fn from(value: T) -> Variant {
value.to_variant()
}
}

impl<T: StaticVariantType> StaticVariantType for Option<T> {
fn static_variant_type() -> Cow<'static, VariantTy> {
let child_type = T::static_variant_type();
Expand Down Expand Up @@ -725,6 +717,18 @@ where
}
}

impl ToVariant for Variant {
fn to_variant(&self) -> Variant {
self.clone()
}
}

impl FromVariant for Variant {
fn from_variant(variant: &Variant) -> Option<Self> {
Some(variant.clone())
}
}

impl<K: StaticVariantType, V: StaticVariantType> StaticVariantType for DictEntry<K, V> {
fn static_variant_type() -> Cow<'static, VariantTy> {
let key_type = K::static_variant_type();
Expand Down Expand Up @@ -836,7 +840,7 @@ mod tests {
fn $name() {
let mut n = $ty::max_value();
while n > 0 {
let v = Variant::from(n);
let v = n.to_variant();
assert_eq!(v.get(), Some(n));
n /= 2;
}
Expand All @@ -850,9 +854,9 @@ mod tests {
fn $name() {
let mut n = $ty::max_value();
while n > 0 {
let v = Variant::from(n);
let v = n.to_variant();
assert_eq!(v.get(), Some(n));
let v = Variant::from(-n);
let v = (-n).to_variant();
assert_eq!(v.get(), Some(-n));
n /= 2;
}
Expand All @@ -871,31 +875,31 @@ mod tests {
#[test]
fn test_str() {
let s = "this is a test";
let v = Variant::from(s);
let v = s.to_variant();
assert_eq!(v.get_str(), Some(s));
}

#[test]
fn test_string() {
let s = String::from("this is a test");
let v = Variant::from(s.clone());
let v = s.clone().to_variant();
assert_eq!(v.get(), Some(s));
}

#[test]
fn test_eq() {
let v1 = Variant::from("this is a test");
let v2 = Variant::from("this is a test");
let v3 = Variant::from("test");
let v1 = "this is a test".to_variant();
let v2 = "this is a test".to_variant();
let v3 = "test".to_variant();
assert_eq!(v1, v2);
assert!(v1 != v3);
}

#[test]
fn test_hash() {
let v1 = Variant::from("this is a test");
let v2 = Variant::from("this is a test");
let v3 = Variant::from("test");
let v1 = "this is a test".to_variant();
let v2 = "this is a test".to_variant();
let v3 = "test".to_variant();
let mut set = HashSet::new();
set.insert(v1);
assert!(set.contains(&v2));
Expand Down
12 changes: 6 additions & 6 deletions src/variant_dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,24 +217,24 @@ mod test {
#[test]
fn create_roundtrip() {
let dict = VariantDict::default();
let var: Variant = dict.into();
let var: Variant = dict.to_variant();
let _dict2: VariantDict = var.into();
}

#[test]
fn create_populate_destroy() {
let dict = VariantDict::default();
dict.insert_value("one", &(1u8.into()));
assert_eq!(dict.lookup_value("one", None), Some(1u8.into()));
dict.insert_value("one", &(1u8.to_variant()));
assert_eq!(dict.lookup_value("one", None), Some(1u8.to_variant()));
}

#[test]
fn create_populate_roundtrip() {
let dict = VariantDict::default();
dict.insert_value("one", &(1u8.into()));
let var: Variant = dict.into();
dict.insert_value("one", &(1u8.to_variant()));
let var: Variant = dict.to_variant();
let dict = VariantDict::from_variant(&var).expect("Not a dict?");
assert_eq!(dict.lookup_value("one", None), Some(1u8.into()));
assert_eq!(dict.lookup_value("one", None), Some(1u8.to_variant()));
}

#[test]
Expand Down

0 comments on commit 633b61a

Please sign in to comment.