Skip to main content

Module protocol

Module protocol 

Source
Expand description

Protocol utilities for third-party crate interoperability.

This module provides utilities for checking renderability, casting objects, and converting types to renderable representations. It implements Rust equivalents of Python’s __gilt__ protocol from the library.

§The __gilt__ Protocol

In Python’s library, objects can implement a __gilt__ method that returns a renderable representation. This module brings that same concept to Rust through the GiltCast trait.

§Key Types

  • GiltCast - The core trait for objects that can be converted to renderables. Implement this on your types to make them printable with gilt.
  • IntoRenderable - A conversion trait that allows any GiltCast type to be converted to a Box<dyn Renderable>.
  • gilt_cast - Attempt to downcast a Box<dyn Any> to a concrete renderable type.
  • RenderableBox - A type-erased wrapper for renderable values.

§Examples

use gilt::protocol::{GiltCast, IntoRenderable};
use gilt::prelude::*;

// Implement GiltCast for a custom type
struct MyData {
    name: String,
    value: i32,
}

impl GiltCast for MyData {
    fn __gilt__(self) -> Box<dyn gilt::console::Renderable> {
        let text = Text::from(format!("{} = {}", self.name, self.value));
        Box::new(Panel::new(text))
    }
}

// Now MyData can be converted to a renderable
let data = MyData { name: "count".into(), value: 42 };
let renderable = data.into_renderable();

Structs§

RenderableBox
A type-erased wrapper that can hold any renderable value.

Traits§

GiltCast
Trait for types that can be converted to a renderable representation.
IntoRenderable
Trait for types that can be converted into a Box<dyn Renderable>.
RenderableExt
Extension trait for types that implement Renderable.

Functions§

as_renderable_mut
Attempt to cast a mutable reference to a renderable trait object.
as_renderable_ref
Attempt to cast a reference to a renderable trait object.
gilt_cast
Attempt to cast a Box<dyn Any> to a concrete renderable type.
is_type
Check if a value is a specific renderable type.