What is the rust way to programmatically instantiate concrete structs of known types?

I’d like to create new instances of structs based on a type given by the caller.

Given two structs: GoodBook and BadBook, I want to wrap those in a type specific Printer. Each book will have its own printer, ie: GoodBookPrinter or BadBookPrinter. Then I’d like a CollectionPrinter that can accept a Vec<GoodBook> or Vec<BadBook> and create a Vec<GoodBookPrinter<GoodBook>> or Vec<BadBookPrinter<BadBook>>.

The caller to CollectionPrinter will know which type of Printer and Book it needs, but I’m confused on how to implement this with traits/generics.

What is the rust way to do this using static dispatch?

My failed attempt:

struct GoodBook {
    id: u32,
}

struct BadBook {
    id: u32,
}

struct GoodBookPrinter<T> {
    // This doesn't actually need to be generic.
    // I'd like for this to be:
    // target: GoodBook
    target: T,
}

trait Printer<T> {
    // All printers should be new-able from the DefaultCollectionPrinter
    fn new(target: T) -> Self;
}

impl<T> Printer<T> for GoodBookPrinter<T> {
    // Compiler would start to complain here if GoodBookPrinter was defined as:
    // target: GoodBook
    // so I've made it generic.
    fn new(target: T) -> Self {
        Self {
            target
        }
    }
}

// The vec will hold Printer<GoodBook> or Printer<BadBook> but never both.
// T will always be a struct
// GoodBook and BadBook have nothing in common
// except an id field
struct DefaultCollectionPrinter<T> {
    printers: Vec<T>,
}

// I'm using a trait with an associated type to implement the functionality
// of DefaultCollectionPrinter, which must be wrong, but I don't know a better
// way to have DefaultCollectionPrinter store a Vec<GoodBookPrinter<GoodBook>> or
// Vec<BadBookPrinter<BadBook>>, etc...
trait CollectionStuff<T> {
    // Should work with GoodBookPrinter<GoodBook> and GoodBookPrinter<BadBook>
    type S: Printer<T>;

    // Wrap each book in its associated printer
    fn new(books: Vec<T>) -> DefaultCollectionPrinter<Self::S> {
        let mut printers: Vec<Self::S> = vec![];
        for book in books {
            let printer = Self::S::new(book);
            printers.push(printer);
        }

        DefaultCollectionPrinter::<Self::S> {
            printers
        }
    }
}

// Tell the DefaultCollectionPrinter that it will be working with
// a GoodBookPrinter<GoodBook> ... but how would it also work with
// BadBookPrinter<BadBook>?
impl<T> CollectionStuff<T> for DefaultCollectionPrinter<T> {
    // Error: the trait GoodBookPrinter<GoodBook>: Printer<T> is not satisfied
    type S = GoodBookPrinter<GoodBook>;
}

fn main() {
    // I want to be able to do something like this:
    // create a vec of books and have the DefaultCollectionPrinter
    // create a Vec<GoodBookPrinter<GoodBook>> or Vec<BadBookPrinter<BadBook>>
    // depending on the type of book given.
    let books = vec![GoodBook{ id: 1 }, GoodBook{ id: 2 }];
    let printers = DefaultCollectionPrinter::new(books).printers;
}

New contributor

rust-nub is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật