# Trait glib::object::Cast [−] [src]

    pub trait Cast: IsA<Object> {
        fn upcast<T>(self) -> T
        where
            T: StaticType + UnsafeFrom<ObjectRef> + Wrapper,
            Self: IsA<T>,
        { ... }
        fn downcast<T>(self) -> Result<T, Self>
        where
            Self: Sized + Downcast<T>,
        { ... }
        fn is<T>(&self) -> bool
        where
            T: StaticType,
        { ... }
        fn dynamic_cast<T>(self) -> Result<T, Self>
        where
            T: StaticType + UnsafeFrom<ObjectRef> + Wrapper,
        { ... }
    }

Upcasting and downcasting support.

Provides conversions up and down the class hierarchy tree.

## Provided Methods

### `fn upcast<T>(self) -> T where
T: StaticType + UnsafeFrom<ObjectRef> + Wrapper,
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

ⓘThis example is not tested

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

### `fn downcast<T>(self) -> Result<T, Self> where
Self: Sized + Downcast<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 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

ⓘThis example is not tested

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

### `fn is<T>(&self) -> bool where
T: StaticType,`

Returns `true` if the object is an instance of (can be cast to) `T`.

### `fn dynamic_cast<T>(self) -> Result<T, Self> where
T: StaticType + UnsafeFrom<ObjectRef> + Wrapper,`

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 `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 as to be performed at runtime.

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

# Example

ⓘThis example is not tested

    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());

## Implementors

- `impl<T: IsA<Object>> Cast for T`

# Help

## Keyboard Shortcuts

?
  Show this help dialog
S
  Focus the search field
↑
  Move up in search results
↓
  Move down in search results
↹
  Switch tab
⏎
  Go to active search result
+ / -
  Collapse/expand all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)