Skip to content

Commit

Permalink
Merge pull request #1058 from sdroege/0.17-backports
Browse files Browse the repository at this point in the history
0.17 backports
  • Loading branch information
sdroege authored Mar 15, 2023
2 parents 658e0bc + b53499b commit 7a9e54c
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 6 deletions.
8 changes: 8 additions & 0 deletions glib-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,14 @@ pub fn derive_props(input: TokenStream) -> TokenStream {
/// }
/// }
/// }
/// impl From<MyEnum> for u32 {
/// fn from(v: MyEnum) -> Self {
/// match v {
/// MyEnum::Zero => 0,
/// MyEnum::NotZero(x) => x
/// }
/// }
/// }
///
/// let myv = MyEnum::NotZero(34);
/// let convertedv = myv.to_value();
Expand Down
4 changes: 2 additions & 2 deletions glib-macros/src/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ fn expand_property_fn(props: &[PropDesc]) -> TokenStream2 {
id: usize,
pspec: &#crate_ident::ParamSpec
) -> #crate_ident::Value {
let prop = DerivedPropertiesEnum::try_from(id-1)
let prop: DerivedPropertiesEnum = std::convert::TryFrom::try_from(id-1)
.unwrap_or_else(|_| panic!("property not defined {}", pspec.name()));
match prop {
#(#match_branch_get,)*
Expand Down Expand Up @@ -470,7 +470,7 @@ fn expand_set_property_fn(props: &[PropDesc]) -> TokenStream2 {
value: &#crate_ident::Value,
pspec: &#crate_ident::ParamSpec
){
let prop = DerivedPropertiesEnum::try_from(id-1)
let prop: DerivedPropertiesEnum = std::convert::TryFrom::try_from(id-1)
.unwrap_or_else(|_| panic!("property not defined {}", pspec.name()));
match prop {
#(#match_branch_set,)*
Expand Down
7 changes: 7 additions & 0 deletions glib-macros/src/value_delegate_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ pub fn impl_value_delegate(input: ValueDelegateInput) -> syn::Result<proc_macro:
<#delegated_ty as #crate_ident::types::StaticType>::static_type()
}
}

impl #crate_ident::value::ToValue for #ident {
fn to_value(&self) -> #crate_ident::value::Value {
let this = self;
Expand All @@ -169,6 +170,12 @@ pub fn impl_value_delegate(input: ValueDelegateInput) -> syn::Result<proc_macro:
}
}

impl From<#ident> for #crate_ident::value::Value {
fn from(this: #ident) -> Self {
#crate_ident::value::Value::from(#delegate_value)
}
}

#to_value_optional

unsafe impl<'a> #crate_ident::value::FromValue<'a> for #ident {
Expand Down
142 changes: 138 additions & 4 deletions glib-macros/tests/value_delegate_derive.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,144 @@
use glib::{value::FromValue, HasParamSpec, StaticType, ToValue};
use glib::{value::FromValue, StaticType, ToValue, Value, ValueDelegate};

#[test]
fn into_value() {
fn test_func(_: impl Into<Value>) {}

#[derive(ValueDelegate)]
pub struct Test(i32);

#[derive(ValueDelegate)]
#[value_delegate(nullable)]
pub struct TestNullable(String);

#[derive(ValueDelegate)]
#[value_delegate(from = i64)]
pub struct TestManualFrom(i32);

impl From<i64> for TestManualFrom {
fn from(v: i64) -> Self {
Self(v as i32)
}
}
impl<'a> From<&'a TestManualFrom> for i64 {
fn from(v: &'a TestManualFrom) -> Self {
v.0 as i64
}
}
impl From<TestManualFrom> for i64 {
fn from(v: TestManualFrom) -> Self {
v.0 as i64
}
}

test_func(&Test(123));
test_func(Test(123));

test_func(&TestManualFrom(123));
test_func(TestManualFrom(123));

test_func(&TestNullable("foo".to_string()));
test_func(TestNullable("foo".to_string()));
test_func(Some(&TestNullable("foo".to_string())));
test_func(&Some(TestNullable("foo".to_string())));
test_func(Some(TestNullable("foo".to_string())));

assert_eq!(glib::Value::from(Test(123)).get::<Test>().unwrap().0, 123);
assert_eq!(glib::Value::from(123).get::<Test>().unwrap().0, 123);
assert_eq!(glib::Value::from(Test(123)).get::<i32>().unwrap(), 123);

assert_eq!(
glib::Value::from(TestManualFrom(123))
.get::<TestManualFrom>()
.unwrap()
.0,
123
);
assert_eq!(
glib::Value::from(123_i64)
.get::<TestManualFrom>()
.unwrap()
.0,
123
);
assert_eq!(
glib::Value::from(TestManualFrom(123)).get::<i64>().unwrap(),
123
);

// From TestNullable
assert_eq!(
glib::Value::from(TestNullable("foo".to_string()))
.get::<Option<TestNullable>>()
.unwrap()
.unwrap()
.0,
"foo"
);
assert_eq!(
glib::Value::from("foo")
.get::<Option<TestNullable>>()
.unwrap()
.unwrap()
.0,
"foo"
);
assert_eq!(
glib::Value::from(TestNullable("foo".to_string()))
.get::<Option<String>>()
.unwrap()
.unwrap(),
"foo"
);
// From Option<TestNullable> Some
assert_eq!(
glib::Value::from(Some(TestNullable("foo".to_string())))
.get::<Option<TestNullable>>()
.unwrap()
.unwrap()
.0,
"foo"
);
assert_eq!(
glib::Value::from(Some("foo"))
.get::<Option<TestNullable>>()
.unwrap()
.unwrap()
.0,
"foo"
);
assert_eq!(
glib::Value::from(Some(TestNullable("foo".to_string())))
.get::<Option<String>>()
.unwrap()
.unwrap(),
"foo"
);
// From Option<TestNullable> None
assert!(glib::Value::from(None::<TestNullable>)
.get::<Option<TestNullable>>()
.unwrap()
.is_none());
assert!(glib::Value::from(None::<String>)
.get::<Option<TestNullable>>()
.unwrap()
.is_none());
assert!(glib::Value::from(None::<TestNullable>)
.get::<Option<String>>()
.unwrap()
.is_none());
}

#[test]
fn higher_level_types() {
#[derive(Debug, glib::ValueDelegate)]
#[derive(Debug, ValueDelegate)]
pub struct MyVec(Vec<String>);

#[derive(Debug, glib::ValueDelegate)]
#[derive(Debug, ValueDelegate)]
#[value_delegate(nullable)]
pub struct MyString(Box<str>);

#[derive(Debug, glib::ValueDelegate)]
#[derive(Debug, ValueDelegate)]
#[value_delegate(from = Option<String>)]
struct MyVecManualFrom(Vec<String>);

Expand All @@ -23,6 +152,11 @@ fn higher_level_types() {
v.0.iter().next().cloned()
}
}
impl From<MyVecManualFrom> for Option<String> {
fn from(v: MyVecManualFrom) -> Self {
v.0.into_iter().next()
}
}

let vec = vec!["foo".to_string(), "bar".to_string()];
let vec_value = vec.to_value();
Expand Down
2 changes: 2 additions & 0 deletions glib/src/property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,9 @@ macro_rules! impl_atomic {
impl_atomic!(std::sync::atomic::AtomicBool, bool);
impl_atomic!(std::sync::atomic::AtomicI8, i8);
impl_atomic!(std::sync::atomic::AtomicI32, i32);
#[cfg(target_has_atomic = "64")]
impl_atomic!(std::sync::atomic::AtomicI64, i64);
impl_atomic!(std::sync::atomic::AtomicU8, u8);
impl_atomic!(std::sync::atomic::AtomicU32, u32);
#[cfg(target_has_atomic = "64")]
impl_atomic!(std::sync::atomic::AtomicU64, u64);

0 comments on commit 7a9e54c

Please sign in to comment.