Skip to content
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

Cursor<SmallVec<[u8; N]>> does not implement Write #108

Closed
rivertam opened this issue Jul 31, 2018 · 2 comments
Closed

Cursor<SmallVec<[u8; N]>> does not implement Write #108

rivertam opened this issue Jul 31, 2018 · 2 comments

Comments

@rivertam
Copy link

@rivertam rivertam commented Jul 31, 2018

I'd be pretty surprised if this weren't implemented for no good reason, but why is SmallVec not a Read? I'd like to be able to use it with the byteorder create like this:

    let mut cursor = std::io::Cursor::new(SmallVec::<[u8; 8]>::new());
    cursor.write_u16::<LittleEndian>(rand::random()).unwrap();
    cursor.write_u16::<LittleEndian>(rand::random()).unwrap();
    cursor.write_u16::<LittleEndian>(rand::random()).unwrap();
    cursor.write_u16::<LittleEndian>(rand::random()).unwrap();

    cursor.set_position(0);

    cursor.read_f64::<LittleEndian>().unwrap()

but this requires that SmallVec implement the Read trait.

@mbrubeck
Copy link
Contributor

@mbrubeck mbrubeck commented Aug 4, 2018

Cursor<SmallVec<[u8; 8]>> already implements the Read trait, thanks to this generic implementation in libstd:

impl<T> Read for Cursor<T> where T: AsRef<[u8]>

However, Cursor<SmallVec> does not implement the Write trait, since there's no similar generic implementation. Fortunately you don't need a Cursor to write to a SmallVec. You can do this:

    let mut v = SmallVec::<[u8; 8]>::new();
    v.write_u16::<LittleEndian>(rand::random()).unwrap();
    v.write_u16::<LittleEndian>(rand::random()).unwrap();
    v.write_u16::<LittleEndian>(rand::random()).unwrap();
    v.write_u16::<LittleEndian>(rand::random()).unwrap();

    let mut cursor = Cursor::new(v);
    cursor.read_f64::<LittleEndian>().unwrap()

or even this, since &[u8] implements Read without needing a Cursor:

    let mut v = SmallVec::<[u8; 8]>::new();
    v.write_u16::<LittleEndian>(rand::random()).unwrap();
    v.write_u16::<LittleEndian>(rand::random()).unwrap();
    v.write_u16::<LittleEndian>(rand::random()).unwrap();
    v.write_u16::<LittleEndian>(rand::random()).unwrap();

    v.as_slice().read_f64::<LittleEndian>().unwrap()
@mbrubeck mbrubeck changed the title Implement Read trait? Cursor<SmallVec<[u8; N]>> does not implement Write Aug 4, 2018
@rivertam
Copy link
Author

@rivertam rivertam commented Aug 5, 2018

@mbrubeck Thanks! The error messages I got here were a little confusing because they seemed to imply it was a read problem, not a write problem.

@rivertam rivertam closed this Aug 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.