Skip to content

Commit 3221869

Browse files
authored
Merge pull request gtk-rs#713 from ranfdev/borrow
Implement Borrow on object types
2 parents 2d50b47 + e13752f commit 3221869

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

glib/src/object.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,7 @@ macro_rules! glib_object_wrapper {
748748
}
749749
}
750750

751+
751752
impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? std::fmt::Debug for $name $(<$($generic),+>)? {
752753
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
753754
f.debug_struct(stringify!($name)).field("inner", &self.inner).finish()
@@ -1172,6 +1173,13 @@ macro_rules! glib_object_wrapper {
11721173
$crate::object::Cast::upcast_ref(self)
11731174
}
11741175
}
1176+
1177+
#[doc(hidden)]
1178+
impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? std::borrow::Borrow<$super_name> for $name $(<$($generic),+>)? {
1179+
fn borrow(&self) -> &$super_name {
1180+
$crate::object::Cast::upcast_ref(self)
1181+
}
1182+
}
11751183
};
11761184

11771185
(@munch_impls $name:ident $(<$($generic:ident $(: $bound:tt $(+ $bound2:tt)*)?),+>)?, $super_name:path, $($implements:tt)*) => {
@@ -1241,6 +1249,13 @@ macro_rules! glib_object_wrapper {
12411249
}
12421250
}
12431251

1252+
#[doc(hidden)]
1253+
impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? std::borrow::Borrow<$crate::object::Object> for $name $(<$($generic),+>)? {
1254+
fn borrow(&self) -> &$crate::object::Object {
1255+
$crate::object::Cast::upcast_ref(self)
1256+
}
1257+
}
1258+
12441259
#[doc(hidden)]
12451260
unsafe impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::object::IsA<$crate::object::Object> for $name $(<$($generic),+>)? { }
12461261

@@ -4659,4 +4674,15 @@ mod tests {
46594674

46604675
assert_eq!(obj1.as_ptr(), obj2.as_ptr());
46614676
}
4677+
4678+
#[test]
4679+
fn test_borrow_hashing() {
4680+
let mut m = std::collections::HashSet::new();
4681+
let boxed_object = crate::BoxedAnyObject::new("");
4682+
4683+
m.insert(boxed_object.clone());
4684+
4685+
let object: &Object = std::borrow::Borrow::borrow(&boxed_object);
4686+
assert_eq!(m.get(object), Some(&boxed_object));
4687+
}
46624688
}

0 commit comments

Comments
 (0)