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

Implement Read/Write on Cursor over &mut Vec #30132

Closed
dbrodie opened this issue Dec 1, 2015 · 4 comments · Fixed by #46830
Closed

Implement Read/Write on Cursor over &mut Vec #30132

dbrodie opened this issue Dec 1, 2015 · 4 comments · Fixed by #46830
Labels
C-feature-accepted Category: A feature request that has been accepted pending implementation. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@dbrodie
Copy link

dbrodie commented Dec 1, 2015

It is currently impossible to use a std::io::Cursor over a Vec in a smart pointer (Mutex, RefCell, ...) in a sane way. Please implement Read/Write/... over Cursor<&'a mut Vec>.

Discussed on users.rust-lang.org.

@est31
Copy link
Member

est31 commented Jan 29, 2017

Good workaround is to call the as_mut_slice function on the mutable Vec reference when creating the Cursor. Then you'll have Cursor<&'a mut[..]> and Read/Write is implemented.

@nox
Copy link
Contributor

nox commented Jan 29, 2017

AFAICT, adding such an impl is a breaking change:

use std::marker::PhantomData;

struct Cursor<T>(PhantomData<T>);

impl<T> Cursor<T> {
    fn new(_: T) -> Self {
        Cursor(PhantomData)
    }
}

trait Write {
    fn flush(&mut self) {}
}

impl<'a> Write for Cursor<&'a mut [u8]> {}
impl Write for Cursor<Vec<u8>> {}

// This code doesn't type check if this is uncommented:
// impl<'a> Write for Cursor<&'a mut Vec<u8>> {}

fn main() {
    let mut vec = vec![0u8];
    Cursor::new(vec.as_mut()).flush();
}

@sfackler
Copy link
Member

New implementations are allowed to cause inference breakage.

@steveklabnik steveklabnik added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed A-libs labels Mar 24, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-feature-request Category: A feature request, i.e: not implemented / a PR. label Jul 24, 2017
@dtolnay dtolnay added C-feature-accepted Category: A feature request that has been accepted pending implementation. and removed C-feature-request Category: A feature request, i.e: not implemented / a PR. labels Nov 18, 2017
@dtolnay
Copy link
Member

dtolnay commented Nov 18, 2017

Seems reasonable. I would be interested in seeing a PR that implements this.

bors added a commit that referenced this issue Jan 10, 2018
Implement `Write` for `Cursor<&mut Vec<T>>`

Fixes #30132

r? @dtolnay (I'm just going through `feature-accepted` issues I swear 😛)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-accepted Category: A feature request that has been accepted pending implementation. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants