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

Add CastNone trait #843

Merged
merged 5 commits into from Dec 4, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 2 additions & 2 deletions glib/src/lib.rs
Expand Up @@ -29,8 +29,8 @@ pub use self::bytes::Bytes;
pub use self::closure::{Closure, RustClosure};
pub use self::error::{BoolError, Error};
pub use self::object::{
BorrowedObject, Cast, Class, InitiallyUnowned, Interface, IsA, Object, ObjectExt, ObjectType,
SendWeakRef, WeakRef,
BorrowedObject, Cast, CastNone, Class, InitiallyUnowned, Interface, IsA, Object, ObjectExt,
ObjectType, SendWeakRef, WeakRef,
};
pub use self::signal::{
signal_handler_block, signal_handler_disconnect, signal_handler_unblock,
Expand Down
69 changes: 69 additions & 0 deletions glib/src/object.rs
Expand Up @@ -288,6 +288,75 @@ pub trait Cast: ObjectType {

impl<T: ObjectType> Cast for T {}

// rustdoc-stripper-ignore-next
/// Convenience trait mirroring `Cast`, implemented on `Option<Object>` types.
ranfdev marked this conversation as resolved.
Show resolved Hide resolved
///
/// # Example
/// ```ignore
/// let widget: Option<Widget> = list_item.child();
///
/// // Without using `CastNone`
/// let label = widget.unwrap().downcast::<gtk::Label>().unwrap();
///
/// // Using `CastNone` we can avoid the first `unwrap()` call
/// let label = widget.downcast::<gtk::Label>().unwrap();
/// ````
pub trait CastNone: Sized {
ranfdev marked this conversation as resolved.
Show resolved Hide resolved
type Inner;
fn downcast<T: ObjectType>(self) -> Result<T, Self>
sdroege marked this conversation as resolved.
Show resolved Hide resolved
bilelmoussaoui marked this conversation as resolved.
Show resolved Hide resolved
where
Self::Inner: CanDowncast<T>;
fn downcast_ref<T: ObjectType>(&self) -> Option<&T>
where
Self::Inner: CanDowncast<T>;
fn upcast<T: ObjectType>(self) -> Option<T>
where
Self::Inner: IsA<T>;
fn upcast_ref<T: ObjectType>(&self) -> Option<&T>
where
Self::Inner: IsA<T>;
ranfdev marked this conversation as resolved.
Show resolved Hide resolved
fn dynamic_cast<T: ObjectType>(self) -> Result<T, Self>;
fn dynamic_cast_ref<T: ObjectType>(&self) -> Option<&T>;
}
impl<I: ObjectType + Sized> CastNone for Option<I> {
type Inner = I;

fn downcast<T: ObjectType>(self) -> Result<T, Self>
where
Self::Inner: CanDowncast<T>,
{
self.ok_or(None)
.and_then(|i| i.downcast().map_err(|e| Some(e)))
}

fn downcast_ref<T: ObjectType>(&self) -> Option<&T>
where
Self::Inner: CanDowncast<T>,
{
self.as_ref().and_then(|i| i.downcast_ref())
}
fn upcast<T: ObjectType>(self) -> Option<T>
where
Self::Inner: IsA<T>,
{
self.map(|i| i.upcast())
}

fn upcast_ref<T: ObjectType>(&self) -> Option<&T>
where
Self::Inner: IsA<T>,
{
self.as_ref().map(|i| i.upcast_ref())
}
fn dynamic_cast<T: ObjectType>(self) -> Result<T, Self> {
self.ok_or(None)
.and_then(|i| i.dynamic_cast().map_err(|e| Some(e)))
}
fn dynamic_cast_ref<T: ObjectType>(&self) -> Option<&T> {
self.as_ref().and_then(|i| i.dynamic_cast_ref())
}
}

// rustdoc-stripper-ignore-next
/// Marker trait for the statically known possibility of downcasting from `Self` to `T`.
pub trait CanDowncast<T> {}
Expand Down
2 changes: 1 addition & 1 deletion glib/src/prelude.rs
Expand Up @@ -5,6 +5,6 @@

pub use crate::param_spec::ParamSpecBuilderExt;
pub use crate::{
Cast, Continue, IsA, ObjectExt, ObjectType, ParamSpecType, StaticType, StaticTypeExt,
Cast, CastNone, Continue, IsA, ObjectExt, ObjectType, ParamSpecType, StaticType, StaticTypeExt,
StaticVariantType, ToSendValue, ToValue, ToVariant,
};