Can I get a trait object of a multi-trait instance without using a generic type?

You can create an empty trait that merges those two traits:

use std::io::{Read, Seek};

trait SeekRead: Seek + Read {}
impl<T: Seek + Read> SeekRead for T {}

fn user_dynamic(stream: &mut SeekRead) {}

This will create a new vtable for SeekRead that contains all the function pointers of both Seek and Read.

You will not be able to cast your &mut SeekRead to either &mut Seek or &mut Read without some trickery (see Why doesn’t Rust support trait object upcasting?)

Leave a Comment