简体   繁体   中英

Unable to add trait bounds to member of a struct

I'm attempting to add use std::io::Cursor with a generic type R , but retain the Read type bounds so that Read trait is accessible, and subsequently can support bytes() method.

Here is my struct definition so far:

struct Parse<'parse, R: Read + BufRead + 'parse> {
    tokens: Vec<Token>,
    source: Cursor<&'parse mut R>,
}

Assuming I have a variable parser that's an instance of Parse , I want to be able to call parser.source.bytes() . bytes() is a method provided by Read . Despite the annotation around R , the compiler is telling me that R isn't satisfying the std::io::Read trait bounds.

Here is that snippet of code in context along with a direct link to the playground :

// using Cursor because it tracks position internally
use std::io::{Cursor, Read, BufRead};

struct Token {
    start: usize,
    end: usize,
}

struct Parse<'parse, R: Read + BufRead + 'parse> {
    tokens: Vec<Token>,
    source: Cursor<&'parse mut R>,
}

impl<'parse, R: Read + BufRead + 'parse> Parse <'parse, R> {
    fn new(source: &'parse mut R) -> Self {
        Parse {
            tokens: vec!(),
            source: Cursor::new(source),
        }
    }

    fn parse_primitive(&mut self) -> std::io::Result<Token> {
        let start = self.source.position();
        let bytes = self.source.bytes();                     // <- error raised here

        // dummy work
        for _ in 0..3 {
            let byte = bytes.next().unwrap().unwrap()
        }
        let end = self.source.position();
        Ok(Token { start: start as usize, end: end as usize})
    }
}

fn main() {
    //...
}

Generates the following error message:

error[E0599]: no method named `bytes` found for type `std::io::Cursor<&'parse mut R>` in the current scope
  --> src/main.rs:24:33
   |
24 |         let bytes = self.source.bytes();
   |                                 ^^^^^
   |
   = note: the method `bytes` exists but the following trait bounds were not satisfied:
           `std::io::Cursor<&mut R> : std::io::Read`
           `&mut std::io::Cursor<&'parse mut R> : std::io::Read`

Help appreciated!

Check the notes after the error messages:

= note: the method `bytes` exists but the following trait bounds were not satisfied:
        `std::io::Cursor<&mut R> : std::io::Read`
        `&mut std::io::Cursor<&'parse mut R> : std::io::Read`

The Read implementation for Cursor<T> is only defined when T: AsRef<[u8]> . If you add that constraint then the Read implementation will be available:

impl<'parse, R: AsRef<[u8]> + BufRead + 'parse> Parse <'parse, R> {

You will still have a few other errors in that code. But this should answer your surface question.

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