Missing implementation of Cursor for a DerefMut of Vec?

I want to use a std::io::Cursor on a Vec<u8> sitting behind a Mutex to gain the Read/Write traits on that Vec. Yet since Read/Write/... are only implemented on Cursor<Vec<u8>> and not on Cursor<T: DerefMut<Target=Vec<u8>>> I do not see any way for me to use it.

I could just copy paste the code the implements Cursor<<Vec<u8>> and just reimplement it, but that seems less than ideal. Shouldn't Cursor be implemented on DerefMut? Or at least on &mut Vec<u8>>?

Note: Read/Write is implemented on Cursor<&'a mut [u8]> but that, obviously, doesn't allow the Write impl to actually change the size of the slice making it less than ideal.

edit: Fixed formatting

You might want to edit your post to fix the type names; it's currently pretty difficult to read.

Implementing Write for Cursor<&mut Vec<u8>> or something like that probably makes sense; please file a bug.

Oh, also, if you want a workaround, std::mem::replace (https://doc.rust-lang.org/nightly/std/mem/fn.replace.html) can be useful if you want to temporarily move a Vec out of a Mutex.

Reported, Thanks for the work around, I haven't thought of that.

I know this is an old question - but I almost made a new one because I ran into a similar issue. In my case, I wanted a Cursor<Arc<Vec<u8>>> that implements Read - but Cursor wasn't "reaching through" the Arc for the AsRef<[u8]> impl it needed on the vector.

The solution I found (which I found here) was to define a newtype that wraps Arc<Vec<u8>> and implement AsRef directly on that.

Not sure if that would work for the above case, but hopefully it helps someone else. (Particularly if they're trying to play sounds in rodio without cloning the audio.)

1 Like