Skip to content

Commit bb27866

Browse files
authored
Merge pull request #1044 from SeaDve/box-str-value
glib: impl StaticType, FromValue, ToValue, HasParamSpec for Box<str>
2 parents 6bdbfe0 + 36d3fb1 commit bb27866

File tree

3 files changed

+135
-2
lines changed

3 files changed

+135
-2
lines changed

glib-macros/tests/value_delegate_derive.rs

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ fn higher_level_types() {
55
#[derive(Debug, glib::ValueDelegate)]
66
pub struct MyVec(Vec<String>);
77

8+
#[derive(Debug, glib::ValueDelegate)]
9+
#[value_delegate(nullable)]
10+
pub struct MyString(Box<str>);
11+
812
#[derive(Debug, glib::ValueDelegate)]
913
#[value_delegate(from = Option<String>)]
1014
struct MyVecManualFrom(Vec<String>);
@@ -23,7 +27,6 @@ fn higher_level_types() {
2327
let vec = vec!["foo".to_string(), "bar".to_string()];
2428
let vec_value = vec.to_value();
2529
let my_vec_value = MyVec(vec.clone()).to_value();
26-
2730
assert_eq!(MyVec::static_type(), Vec::<String>::static_type());
2831
assert_eq!(
2932
vec_value.get::<Vec<String>>().unwrap(),
@@ -38,10 +41,88 @@ fn higher_level_types() {
3841
unsafe { MyVec::from_value(&my_vec_value).0 }
3942
);
4043

44+
let string = "foo".to_string();
45+
let string_value = string.to_value();
46+
let my_string_value = MyString(string.into()).to_value();
47+
assert_eq!(MyString::static_type(), Box::<str>::static_type());
48+
assert_eq!(
49+
string_value.get::<Box<str>>().unwrap(),
50+
my_string_value.get::<Box<str>>().unwrap(),
51+
);
52+
assert_eq!(string_value.value_type(), my_string_value.value_type());
53+
assert_eq!(unsafe { Box::<str>::from_value(&string_value) }, unsafe {
54+
MyString::from_value(&string_value).0
55+
});
56+
assert_eq!(
57+
unsafe { Box::<str>::from_value(&my_string_value) },
58+
unsafe { MyString::from_value(&my_string_value).0 }
59+
);
60+
61+
let string_some = Some("foo".to_string());
62+
let string_some_value = string_some.to_value();
63+
let string_none_value = None::<String>.to_value();
64+
let my_string_some_value = MyString(string_some.unwrap().into()).to_value();
65+
let my_string_none_value = None::<MyString>.to_value();
66+
assert_eq!(
67+
Option::<MyString>::static_type(),
68+
Option::<Box<str>>::static_type()
69+
);
70+
assert_eq!(
71+
string_some_value
72+
.get::<Option<Box<str>>>()
73+
.unwrap()
74+
.unwrap(),
75+
my_string_some_value
76+
.get::<Option<Box<str>>>()
77+
.unwrap()
78+
.unwrap(),
79+
);
80+
assert_eq!(
81+
string_none_value
82+
.get::<Option<Box<str>>>()
83+
.unwrap()
84+
.is_none(),
85+
my_string_none_value
86+
.get::<Option<Box<str>>>()
87+
.unwrap()
88+
.is_none(),
89+
);
90+
assert_eq!(
91+
string_some_value.value_type(),
92+
my_string_some_value.value_type()
93+
);
94+
assert_eq!(
95+
string_none_value.value_type(),
96+
my_string_none_value.value_type()
97+
);
98+
assert_eq!(
99+
unsafe { Option::<Box<str>>::from_value(&string_some_value).unwrap() },
100+
unsafe {
101+
Option::<MyString>::from_value(&string_some_value)
102+
.unwrap()
103+
.0
104+
}
105+
);
106+
assert_eq!(
107+
unsafe { Option::<Box<str>>::from_value(&string_none_value).is_none() },
108+
unsafe { Option::<MyString>::from_value(&string_none_value).is_none() }
109+
);
110+
assert_eq!(
111+
unsafe { Option::<Box<str>>::from_value(&my_string_some_value).unwrap() },
112+
unsafe {
113+
Option::<MyString>::from_value(&my_string_some_value)
114+
.unwrap()
115+
.0
116+
}
117+
);
118+
assert_eq!(
119+
unsafe { Option::<Box<str>>::from_value(&my_string_none_value).is_none() },
120+
unsafe { Option::<MyString>::from_value(&my_string_none_value).is_none() }
121+
);
122+
41123
let opt = Some("foo".to_string());
42124
let opt_value = opt.to_value();
43125
let my_vec_manual_from_value = MyVecManualFrom::from(opt).to_value();
44-
45126
assert_eq!(
46127
MyVecManualFrom::static_type(),
47128
Option::<String>::static_type()

glib/src/param_spec.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2115,6 +2115,15 @@ impl HasParamSpec for String {
21152115
Self::ParamSpec::builder
21162116
}
21172117
}
2118+
impl HasParamSpec for Box<str> {
2119+
type ParamSpec = ParamSpecString;
2120+
type SetValue = str;
2121+
type BuilderFn = fn(&str) -> ParamSpecStringBuilder;
2122+
2123+
fn param_spec_builder() -> Self::BuilderFn {
2124+
Self::ParamSpec::builder
2125+
}
2126+
}
21182127
impl HasParamSpec for crate::StrV {
21192128
type ParamSpec = ParamSpecBoxed;
21202129
type SetValue = Self;

glib/src/value.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,49 @@ impl ToValueOptional for String {
904904
}
905905
}
906906

907+
impl ValueType for Box<str> {
908+
type Type = String;
909+
}
910+
911+
impl ValueTypeOptional for Box<str> {}
912+
913+
unsafe impl<'a> FromValue<'a> for Box<str> {
914+
type Checker = GenericValueTypeOrNoneChecker<Self>;
915+
916+
unsafe fn from_value(value: &'a Value) -> Self {
917+
Box::<str>::from(<&str>::from_value(value))
918+
}
919+
}
920+
921+
impl StaticType for Box<str> {
922+
fn static_type() -> Type {
923+
String::static_type()
924+
}
925+
}
926+
927+
impl ToValue for Box<str> {
928+
fn to_value(&self) -> Value {
929+
<&str>::to_value(&self.as_ref())
930+
}
931+
932+
fn value_type(&self) -> Type {
933+
String::static_type()
934+
}
935+
}
936+
937+
impl From<Box<str>> for Value {
938+
#[inline]
939+
fn from(s: Box<str>) -> Self {
940+
s.to_value()
941+
}
942+
}
943+
944+
impl ToValueOptional for Box<str> {
945+
fn to_value_optional(s: Option<&Self>) -> Value {
946+
<str>::to_value_optional(s.as_ref().map(|s| s.as_ref()))
947+
}
948+
}
949+
907950
impl ValueType for Vec<String> {
908951
type Type = Vec<String>;
909952
}

0 commit comments

Comments
 (0)