3 ms·
Slightly OT: while trying to understand the vulnerability I came across a Rust question. Why can you do this? let mut outfd = File::create(&outpath);
by kaoD 12y ago
Slightly OT: while trying to understand the vulnerability I came across a Rust question.
Why can you do this?
let mut outfd = File::create(&outpath);
match outfd.write_all(&buffer[0 .. len]) { ... }
According to `old_io::File`'s doc[0] it returns an `IoResult<File>` which is an alias `type IoResult<T> = Result<T, IoError>` i.e. `Result<File, IoError>`. How come you can do `write_all` directly on a `Result<File, IoError>` without unwrapping the `File` first?
The example in the docs does something similar:
let mut f = File::create(&Path::new("foo.txt"));
f.write(b"This is a sample file");
So I guess I'm missing something here.
[0] http://doc.rust-lang.org/std/old_io/fs/struct.File.html#method.create http://doc.rust-lang.org/std/old_io/fs/struct.File.html#meth...
- steveklabnik 12y agoIt's because Writer (the trait write_all() is from) is implemented on IoResult. http://doc.rust-lang.org/std/old_io/trait.Writer.html#tymethod.write_all http://doc.rust-lang.org/std/old_io/trait.Writer.html#tymeth... impl<W: Writer> Writer for IoResult<W> From the `std::old_io` docs: http://doc.rust-lang.org/std/old_io/index.html http://doc.rust-lang.org/std/old_io/index.html > Common traits are implemented for IoResult, e.g. impl<R: Reader> Reader > for IoResult<R>, so that error values do not have to be 'unwrapped' before use.
- kaoD 12y agoAh, remote implementation of traits bites me once again. Is there any reason behind not listing the implemented traits in IoResult's docs? Listing the implementors in the trait is not very useful since in the first place you have to know which traits are implemented to consult them. It's backwards and counterintuitive as I see it.
- steveklabnik 12y agoIn general, we're still working on a number of usability issues with Rustdoc's output. The one that bits the most people is methods through Deref. Luckily, this is just a tooling issue, a small bug to be fixed, rather than some sort of fundamental problem. The beta cycle is going to be all about polish, and issues like this are a good candidate for that kind of work.
- masklinn 12y agoThe explanation is at http://doc.rust-lang.org/std/old_io/#error-handling http://doc.rust-lang.org/std/old_io/#error-handling: IoResult implements a bunch of IO traits so you don't need to unwrap it before using it: > Common traits are implemented for IoResult, e.g. impl<R: Reader> Reader for IoResult<R>, so that error values do not have to be 'unwrapped' before use.
- deleted 12y ago[deleted]
- mhaymo 12y agoLine 21 here implements Writer for IOResult<T> where T also implements Writer: https://github.com/rust-lang/rust/blob/master/src/libstd/old_io/result.rs https://github.com/rust-lang/rust/blob/master/src/libstd/old...