Skip to content

Implement Container for some Reader/Writers #12559

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

SiegeLord
Copy link
Contributor

It seems useful to have the ability to query the length of some Reader/Writers. I implemented it for Reader/Writers for which it made the most sense to me. E.g. I omitted TeeReader as it's not clear what length would refer to in that case.

Notably missing is the implementation of this for File, but I don't know if it can be done in general, as the stat function can fail. Maybe this isn't the right approach at all and a new trait is required (akin to Iterator's size_hint).

My use case, incidentally, is to pass a Rust Reader/Writer to a C library custom file interface which, amongst other things, requires me to report the size of the 'file'.

@@ -52,6 +53,12 @@ impl<R: Reader> Reader for LimitReader<R> {
}
}

impl<R> Container for LimitReader<R> {
fn len(&self) -> uint {
self.limit
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this true? What about something like LimitReader::new(BufReader::new([]), 100), where the reader is actually empty?

@alexcrichton
Copy link
Member

I'm a r- on this change. All generic reader/writer wrappers should provide access to their innards for this kind of inspection. Requiring a Container implementation for all wrappers seems like too much of a burden to authors.

I feel like patterns like this encourage implementing all traits for wrappers of types that implement all the same traits, which seems near impossible to do.

@lilyball
Copy link
Contributor

I agree with @alexcrichton.

@SiegeLord
Copy link
Contributor Author

All generic reader/writer wrappers should provide access to their innards for this kind of inspection.

Wasn't that what Decorator was (#11394)? Do you suggest re-introducing it?

@alexcrichton
Copy link
Member

I don't think the Decorator trait should be necessary, it's just one more trait to import that isn't all that necessary. Types should provide some form of accessor via get_ref, get_mut, a public field, etc.

@SiegeLord
Copy link
Contributor Author

I'm not sure how that helps generic code at all.

If all you're concerned about is the burden on the writers of wrappers, then a Decorator trait would be the solution:

trait Decorator<T>
{
    fn get_ref<'l>(&'l self) -> &'l T;
}

impl<T: Container, D: Decorator<T>> Container for D
{
    fn len(&self) -> uint
    {
        self.get_ref().len()
    }
}

@huonw
Copy link
Member

huonw commented Feb 26, 2014

A generic implementation conflicts with all others, so that last impl would mean nothing else can implement Container.

@SiegeLord
Copy link
Contributor Author

Ah... nevermind then. I'll have to think about a solution for my use case that is more palatable...

@SiegeLord SiegeLord closed this Feb 26, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants