Implicit ?Sized on BufReader inherent methods

this impl block lacks a ?Sized bound, but nevertheless, most of the methods can be called when R is ?Sized.

example:

use std::io::{Read, BufReader, stdin};

fn main() {
    let r1 = BufReader::new(stdin());
    let r2: Box<BufReader<dyn Read>> = Box::new(r1);
    println!("{}", r2.capacity());
}

even stranger, into_inner has an explict Sized bound, something which should not be meaningful, but yet here it is.

what's going on? is this edition 2024 semantics leaking into the docs?

If you look more closely, you might notice that the impl block only contains 2 methods … well … more precisely, associated functions: new and with_capacity.

After that starts the next impl block, which does have the ?Sized bound; and that’s also the one that contains into_inner, which thus has its own Sized bound again. (Don’t ask my why they didn’t put it into the first impl block instead, though.)

2 Likes

Multiple impl blocks is an area where rustdoc is not great. You can't know the impl header details from the sidebar, or when you're looking at a method that's not the first in the impl block (without a lot of annoying scrolling and collapsing).

(Header details can include not just bounds, but other things such as specific type constructors.)

And the sidebar is sorted alphabetically to boot, so if you care about a subset of methods that happens to correspond to a specific impl block, you can't tell what they are from the sidebar.

2 Likes

The second block doesn't have an R: Read bound (and ?Read doesn't work).

into_inner used to be in the same block as new, but #58423 split that block up to remove some Read bounds, and later #111074 relaxed some Sized bounds where possible.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.