Trait glib::object::Cast

source ·
pub trait Cast: ObjectType {
    // Provided methods
    fn upcast<T: ObjectType>(self) -> T
       where Self: IsA<T> { ... }
    fn upcast_ref<T: ObjectType>(&self) -> &T
       where Self: IsA<T> { ... }
    fn downcast<T: ObjectType>(self) -> Result<T, Self>
       where Self: MayDowncastTo<T> { ... }
    fn downcast_ref<T: ObjectType>(&self) -> Option<&T>
       where Self: MayDowncastTo<T> { ... }
    fn dynamic_cast<T: ObjectType>(self) -> Result<T, Self> { ... }
    fn dynamic_cast_ref<T: ObjectType>(&self) -> Option<&T> { ... }
    unsafe fn unsafe_cast<T: ObjectType>(self) -> T { ... }
    unsafe fn unsafe_cast_ref<T: ObjectType>(&self) -> &T { ... }
}
Expand description

Upcasting and downcasting support.

Provides conversions up and down the class hierarchy tree.

Provided Methods§

source

fn upcast<T: ObjectType>(self) -> T
where Self: IsA<T>,

Upcasts an object to a superclass or interface T.

NOTE: This statically checks at compile-time if casting is possible. It is not always known at compile-time, whether a specific object implements an interface or not, in which case upcast would fail to compile. dynamic_cast can be used in these circumstances, which is checking the types at runtime.

Example
let button = gtk::Button::new();
let widget = button.upcast::<gtk::Widget>();
source

fn upcast_ref<T: ObjectType>(&self) -> &T
where Self: IsA<T>,

Upcasts an object to a reference of its superclass or interface T.

NOTE: This statically checks at compile-time if casting is possible. It is not always known at compile-time, whether a specific object implements an interface or not, in which case upcast would fail to compile. dynamic_cast can be used in these circumstances, which is checking the types at runtime.

Example
let button = gtk::Button::new();
let widget = button.upcast_ref::<gtk::Widget>();
source

fn downcast<T: ObjectType>(self) -> Result<T, Self>
where Self: MayDowncastTo<T>,

Tries to downcast to a subclass or interface implementor T.

Returns Ok(T) if the object is an instance of T and Err(self) otherwise.

NOTE: This will check at compile-time if T is lower down the inheritance tree of Self, but also check at runtime if downcasting is indeed possible.

Example
let button = gtk::Button::new();
let widget = button.upcast::<gtk::Widget>();
assert!(widget.downcast::<gtk::Button>().is_ok());
source

fn downcast_ref<T: ObjectType>(&self) -> Option<&T>
where Self: MayDowncastTo<T>,

Tries to downcast to a reference of its subclass or interface implementor T.

Returns Some(T) if the object is an instance of T and None otherwise.

NOTE: This will check at compile-time if T is lower down the inheritance tree of Self, but also check at runtime if downcasting is indeed possible.

Example
let button = gtk::Button::new();
let widget = button.upcast::<gtk::Widget>();
assert!(widget.downcast_ref::<gtk::Button>().is_some());
source

fn dynamic_cast<T: ObjectType>(self) -> Result<T, Self>

Tries to cast to an object of type T. This handles upcasting, downcasting and casting between interface and interface implementors. All checks are performed at runtime, while upcast will do many checks at compile-time already. downcast will perform the same checks at runtime as dynamic_cast, but will also ensure some amount of compile-time safety.

It is not always known at compile-time, whether a specific object implements an interface or not, and checking has to be performed at runtime.

Returns Ok(T) if the object is an instance of T and Err(self) otherwise.

Example
let button = gtk::Button::new();
let widget = button.dynamic_cast::<gtk::Widget>();
assert!(widget.is_ok());
let widget = widget.unwrap();
assert!(widget.dynamic_cast::<gtk::Button>().is_ok());
source

fn dynamic_cast_ref<T: ObjectType>(&self) -> Option<&T>

Tries to cast to reference to an object of type T. This handles upcasting, downcasting and casting between interface and interface implementors. All checks are performed at runtime, while downcast and upcast will do many checks at compile-time already.

It is not always known at compile-time, whether a specific object implements an interface or not, and checking has to be performed at runtime.

Returns Some(T) if the object is an instance of T and None otherwise.

Example
let button = gtk::Button::new();
let widget = button.dynamic_cast_ref::<gtk::Widget>();
assert!(widget.is_some());
let widget = widget.unwrap();
assert!(widget.dynamic_cast_ref::<gtk::Button>().is_some());
source

unsafe fn unsafe_cast<T: ObjectType>(self) -> T

Casts to T unconditionally.

Panics

Panics if compiled with debug_assertions and the instance doesn’t implement T.

Safety

If not running with debug_assertions enabled, the caller is responsible for ensuring that the instance implements T

source

unsafe fn unsafe_cast_ref<T: ObjectType>(&self) -> &T

Casts to &T unconditionally.

Panics

Panics if compiled with debug_assertions and the instance doesn’t implement T.

Safety

If not running with debug_assertions enabled, the caller is responsible for ensuring that the instance implements T

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<T: ObjectType> Cast for T