Skip to content

Commit

Permalink
Implement Deref for TemplateChild
Browse files Browse the repository at this point in the history
Allows using template children without .get()
  • Loading branch information
BrainBlasted committed Jan 12, 2021
1 parent 96bc469 commit bc7107e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
1 change: 0 additions & 1 deletion examples/src/bin/composite_template.rs
Expand Up @@ -88,7 +88,6 @@ impl ExApplicationWindow {
let self_ = imp::ExApplicationWindow::from_instance(self);
self_
.subtitle
.get()
.set_text("This is an example window made using composite templates");
}
}
Expand Down
21 changes: 21 additions & 0 deletions gtk4/src/subclass/widget.rs
Expand Up @@ -980,6 +980,27 @@ where
}
}

impl<T> std::ops::Deref for TemplateChild<T>
where
T: ObjectType + FromGlibPtrNone<*mut <T as ObjectType>::GlibType>,
{
type Target = T;

/// # Safety
///
/// Since the template child may not be properly bound,
/// this cast is potentially dangerous if, for example,
/// the template child isn't bound or is of the wrong type.
/// The caller is responsible for ensuring that the template
/// child is bound and of the right type.
fn deref(&self) -> &Self::Target {
unsafe {
assert!(!self.ptr.is_null());
&*(&self.ptr as *const _ as *const T)
}
}
}

impl<T> TemplateChild<T>
where
T: ObjectType + FromGlibPtrNone<*mut <T as ObjectType>::GlibType>,
Expand Down

0 comments on commit bc7107e

Please sign in to comment.