IntoElement

Trait IntoElement 

Source
pub trait IntoElement<'s>: Sized {
    type ElementType: Element<'s>;

    // Required method
    fn into_element(self) -> Self::ElementType;

    // Provided methods
    fn fixed_width(self, width: usize) -> FixedWidth<Self::ElementType> { ... }
    fn styled(self, style: Style) -> Styled<Self::ElementType> { ... }
    fn boxed(self) -> BoxElement<'s> { ... }
}
Expand description

A type that can be converted into an element.

Required Associated Types§

Source

type ElementType: Element<'s>

The element type to be converted into.

Required Methods§

Source

fn into_element(self) -> Self::ElementType

Converts this type into an Element.

Provided Methods§

Source

fn fixed_width(self, width: usize) -> FixedWidth<Self::ElementType>

Convenience function to wrap this element in a FixedWidth.

Examples found in repository?
examples/input.rs (line 25)
13fn main() -> std::io::Result<()> {
14    let stdout = std::io::stdout().into_raw_mode()?;
15    let mut r = Renderer::new(stdout);
16
17    let mut name = String::new();
18
19    let mut events = std::io::stdin().events();
20    loop {
21        r.reset()?
22            .render((
23                "Enter your name: ".into_element(),
24                (name.into_element(), Cursor, Gap(1))
25                    .fixed_width(20)
26                    .truncated(Direction::Left)
27                    .styled(Style::bg(240)),
28            ))?
29            .finish()?;
30
31        let Some(event) = events.next().transpose()? else {
32            break;
33        };
34        match event {
35            Event::Key(Key::Char(ch)) if !ch.is_ascii_control() => name.push(ch),
36            Event::Key(Key::Char('\n' | '\r')) => break,
37            Event::Key(Key::Backspace) => {
38                name.pop();
39            }
40            _ => {}
41        }
42    }
43
44    r.clear()?;
45    drop(r);
46    println!("Your name is {name:?}");
47    Ok(())
48}
Source

fn styled(self, style: Style) -> Styled<Self::ElementType>

Convenience function to wrap this element in a Styled.

Examples found in repository?
examples/tic_tac_toe.rs (line 136)
134fn render_player(player: Option<Player>) -> impl Element<'static> {
135    match player {
136        None => "-".styled(Style::fg(245)),
137        Some(Player::X) => "X".styled(Style::BOLD + Style::fg(33)),
138        Some(Player::O) => "O".styled(Style::BOLD + Style::fg(203)),
139    }
140}
More examples
Hide additional examples
examples/input.rs (line 27)
13fn main() -> std::io::Result<()> {
14    let stdout = std::io::stdout().into_raw_mode()?;
15    let mut r = Renderer::new(stdout);
16
17    let mut name = String::new();
18
19    let mut events = std::io::stdin().events();
20    loop {
21        r.reset()?
22            .render((
23                "Enter your name: ".into_element(),
24                (name.into_element(), Cursor, Gap(1))
25                    .fixed_width(20)
26                    .truncated(Direction::Left)
27                    .styled(Style::bg(240)),
28            ))?
29            .finish()?;
30
31        let Some(event) = events.next().transpose()? else {
32            break;
33        };
34        match event {
35            Event::Key(Key::Char(ch)) if !ch.is_ascii_control() => name.push(ch),
36            Event::Key(Key::Char('\n' | '\r')) => break,
37            Event::Key(Key::Backspace) => {
38                name.pop();
39            }
40            _ => {}
41        }
42    }
43
44    r.clear()?;
45    drop(r);
46    println!("Your name is {name:?}");
47    Ok(())
48}
Source

fn boxed(self) -> BoxElement<'s>

Convenience function to box this element.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<'s> IntoElement<'s> for &'s str

Implementors§

Source§

impl<'s, E: Element<'s>> IntoElement<'s> for E