Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Deref for TemplateChild #163

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,
BrainBlasted marked this conversation as resolved.
Show resolved Hide resolved
/// 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