-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Read::read_exact works with StdinLock, but not with Cursor<StdinLock>? #52470
Comments
A Cursor is meant as a wrapper to Vec or &mut [u8] to be able to write to them. The compiler error says that StdinLock cant be converted to a slice. |
That's not how I understood it from the documentation.
to me that suggests the main purpose is to provide Seek implementation.
If it's only meant to wrap Vec or [u8] I think it would avoid misunderstandings to say so out right.
Again suggesting Cursor is fairly generic about underlying I/O object. (FWIW if asked to name some I/O objects, I'd be pressed to consider Vec and [u8] as such.) ... The compiler error is confusing as at no point is there any (obvious) attempt to convert StdinLock into a slice. It's only ever used through the Read trait. Though looking more closely I now see the fine print later on ( I'd still argue the behavior is surprising. StdinLock implements Read, so does Cursor, there's no obvious reason why Cursor<StdinLock> wouldn't. I can sort of understand why it's currently that way, and can imagine some problems there may be generalizing it, but can't quite see any fundamental reasons for Cursor to be restricted to only Vec and [u8] and the documentation is vague enough to make that seem like a bug / unintended omission rather than intentional design / limitation. |
It is a docs bug. The entire purpose of Cursor is to take a buffer of bytes and turn that into a reader by adding a cursor indicating the current position in the buffer. |
Reduce misconceptions about Cursor being more general than it really is. Fixes: rust-lang#52470
Made a PR to suggest some documentation changes to better describe the intention. I do wonder though if the |
There probably should have been an |
Cursor: update docs to clarify Cursor only works with in-memory buffers Reduce misconceptions about Cursor being more general than it really is. Fixes: rust-lang#52470
I've some code reading a stream from stdin. Wanted to wrap it in a Cursor to have it keep track of the number of bytes read already instead of having to do it manually, but started getting odd compiler errors.
Since
read_exact
works just fine withStdinLock
(andStdin
) shouldn't it work just the same withCursor<StdinLock>
?(Playground)
Errors:
The text was updated successfully, but these errors were encountered: