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

Take R: Read and W: Write by value #7

Closed
dtolnay opened this Issue Apr 4, 2017 · 3 comments

Comments

Projects
None yet
3 participants
@dtolnay
Copy link
Member

dtolnay commented Apr 4, 2017

The standard library contains these two impls:

impl<'a, R: Read + ?Sized> Read for &'a mut R
impl<'a, W: Write + ?Sized> Write for &'a mut W

That means any function that accepts R: Read or W: Write by value can be called with a mut reference if necessary.

Real examples:

// from flate2
impl<R: Read> DecoderReader<R> {
    pub fn new(r: R) -> Self
}

// from serde_json
pub fn from_reader<R, T>(rdr: R) -> Result<T> where R: Read, T: Deserialize

In the documentation of such functions, briefly remind users that a mut reference can be passed. New Rust users often struggle with this ("I want to deserialize two values from the same fs::File but I passed the file by value so I can't use it anymore").

@brson brson added the important label Jun 30, 2017

@brson

This comment has been minimized.

Copy link
Contributor

brson commented Jun 30, 2017

"Interop" section.

@dtolnay

This comment has been minimized.

Copy link
Member Author

dtolnay commented Oct 25, 2017

Written in eceb521.

@dtolnay dtolnay closed this Oct 25, 2017

@lfairy

This comment has been minimized.

Copy link

lfairy commented Nov 8, 2017

Can this be a Clippy lint (if it isn't already)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.