2

I created a structure where an iterator over a file or stdin should be stored, but compiler yelling at me :)

I decided that Lines is the struct I need to store in my struct to iterate using it later and Box will allow to store variable with unknown size, so I define my structure like that:

pub struct A {
    pub input: Box<Lines<BufRead>>,
}

I want to do something like this later:

let mut a = A {
    input: /* don't know what should be here yet */,
};
if something {
    a.input = Box::new(io::stdin().lock().lines());
} else {
    a.input = Box::new(BufReader::new(file).lines());
}

And finally

for line in a.input {
    // ...
}

But I got an error from the compiler

error[E0277]: the size for values of type `(dyn std::io::BufRead + 'static)` cannot be known at compilation time
  --> src/context.rs:11:5
   |
11 |     pub input: Box<Lines<BufRead>>,
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
   |
   = help: the trait `std::marker::Sized` is not implemented for `(dyn std::io::BufRead + 'static)`
   = note: to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch19-04-advanced-types.html#dynamically-sized-types-and-sized>
   = note: required by `std::io::Lines`

How can I achieve my goal?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366

1 Answers1

1

The most generic answer to your question is that you don't / can't. Locking stdin returns a type that references the Stdin value. You cannot create a local value (stdin()), take a reference to it (.lock()), and then return that reference.

If you just want to do this inside of a function without returning it, then you can create a trait object:

use std::io::{self, prelude::*, BufReader};

fn example(file: Option<std::fs::File>) {
    let stdin;
    let mut stdin_lines;
    let mut file_lines;

    let input: &mut Iterator<Item = _> = match file {
        None => {
            stdin = io::stdin();
            stdin_lines = stdin.lock().lines();
            &mut stdin_lines
        }
        Some(file) => {
            file_lines = BufReader::new(file).lines();
            &mut file_lines
        }
    };

    for line in input {
        // ...
    }
}

Or create a new generic function that you can pass either type of concrete iterator to:

use std::io::{self, prelude::*, BufReader};

fn example(file: Option<std::fs::File>) {
    match file {
        None => finally(io::stdin().lock().lines()),
        Some(file) => finally(BufReader::new(file).lines()),
    }
}

fn finally(input: impl Iterator<Item = io::Result<String>>) {
    for line in input {
        // ...
    }
}

You could put either the trait object or the generic type into a structure even though you can't return it:

struct A<'a> {
    input: &mut Iterator<Item = io::Result<String>>,
}
struct A<I> 
where
    I: Iterator<Item = io::Result<String>>,
{
    input: I,
}

If you are feeling adventurous, you might be able to use some unsafe code / crates wrapping unsafe code to store the Stdin value and the iterator referencing it together, which is not universally safe.

See also:

input: Box<Lines<BufRead>>,

This is invalid because Lines is not a trait. You want either:

use std::io::{prelude::*, Lines};

pub struct A {
    pub input: Lines<Box<BufRead>>,
}

Or

use std::io;

pub struct A {
    pub input: Box<Iterator<Item = io::Result<String>>>,
}
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366