Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

rust - Creating a struct with a generic trait for field. Expected struct<trait> found struct<type that implements said trait>

I'm trying to create a struct that has a BufWriter that uses the Write trait, so that this struct could have a buffered writer that can be anything that implements that trait: File, Stream, etc. But I'm having an issue in my function that creates the struct saying that I have mismatched types. Here is an example code with the same issue.

use std::fs::File;
use std::io::{BufWriter, Write};

pub struct BufWriterStruct<W: Write> {
    pub writer: Option<BufWriter<W>>,
}

impl <W: Write>BufWriterStruct<W> {
    pub fn new(filename: &str) -> BufWriterStruct<W> {
        BufWriterStruct {
            writer: Some(BufWriter::new(File::create(filename).unwrap())),
        }
    }
}

fn main() {
    let tmp = BufWriterStruct::new("tmp.txt");
}

Playground

with the error

error: mismatched types:
 expected `BufWriterStruct<W>`,
    found `BufWriterStruct<std::fs::File>`

If instead I change my new function to instead take a parameter that implements the Write trait and use that when creating BufWriter, it works fine.

I feel like the former should be possible to do somehow.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
impl <W: Write>BufWriterStruct<W> {
    pub fn new(filename: &str) -> BufWriterStruct<W>

This signature means that the following code would be valid:

let tmp : BufWriterStruct<Stdout> = BufWriterStruct::new("tmp.txt");

However this would clearly not work with your implementation of new, as it produces a BufWriterStruct<File>, not <StdOut>. If you want to return a BufWriterStruct<File>, you should declare your new function accordingly:

pub fn new(filename: &str) -> BufWriterStruct<File>

However, this change alone will leave the W parameter on the impl block unconstrained, and the compiler will be unable to infer a type for it. The best solution for this would be to put the new method on a non-generic impl:

impl BufWriterStruct<File> {
    pub fn new(filename: &str) -> BufWriterStruct<File> {
        // ...
    }
}

Note that Rust doesn't support overloading (methods with the same name but different parameter lists), so if you had two impl blocks on the same type (disregarding generic parameters) each with a method named new, you'd get an error when trying to invoke one of them (as of Rust 1.4.0, merely defining methods with the same name in separate impl blocks is not a compile-time error). Therefore, you might wish to use a more explicit name than new.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...