简体   繁体   中英

Nested-generics arguments in struct Rust

i am writing a program that uses text user interface, and i moved TUI functionality into the separate module, which contains a struct with TUI logic/variables.

So the declaration of struct is:

struct App<'a, T: io::Write>
where
    T: io::Write,
{
    items_left: StatefulList<String>,
    items_right: StatefulList<String>,
    input_mode: InputMode,
    parser: &'a parser::Parser,
    // --- [ Tui Backend ] ---
    backend: TermionBackend<T>,
}

and the constructor function implementation is:

impl<'a, T> App<'a, T>
where
    T: io::Write,
{
    fn new(parser: &'a parser::Parser) -> App<'a, T> {
        let stdout = io::stdout().into_raw_mode().unwrap();
        let stdout = MouseTerminal::from(stdout);
        let stdout = AlternateScreen::from(stdout);
        let backend = TermionBackend::new(stdout);
        let mut terminal = Terminal::new(backend).unwrap();
        let events = Events::new();

        App {
            items_left: StatefulList::with_items(vec![]),
            items_right: StatefulList::with_items(vec![]),
            input_mode: InputMode::Normal,
            parser,
            backend,
        }
}

But i got the error from rust-analyzer:

mismatched types expected struct TermionBackend<T> found struct TermionBackend<AlternateScreen<MouseTerminal<RawTerminal<Stdout>>>>

I want to use generics, because TermionBackend has a lot of nested classes in it, and its declaration is very huge.

Also i find out in sources that AlternateScreen struct, which is T, implements io::Write trait, and that's why i don't understand why the error occurs.

If you want to avoid typing the same type over and over, you can use a type alias , eg type T = AlternateScreen<MouseTerminal<RawTerminal<Stdout>>> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM