File IO in Rust
blog.safaribooksonline.com
blog.safaribooksonline.com
For example, as shown in the article, the File::open function takes a Path type. The purpose of the Path type is for safe and platform-agnostic path manipulation... but that's really not necessary here. Say you provide a string containing an invalid filepath to the constructor to Path: you're gonna get a runtime failure. This is the same failure mode that you'd get just by providing the string to File::open directly. So ideally the open function would allow you to provide either a Path or a string directly, without forcing you to jump through the hoop. These are the little things that I mean when I say that our I/O APIs haven't yet had the polish they need.
At the end of the day any file-opening function is gonna be reduced to passing a string. Currently, to open a file, we take the path that you give us and call .to_c_str() on it, but that method also exists on strings. With Rust's trait system, we could tell the file-opening function to become generic over any type that implements .to_c_str(), and bam: now it works on both Paths and strings.
In practice it might be a bit of work because I don't know if we actually have a trait for the .to_c_str() method, or if it's currently just implemented in an ad-hoc way on types that need it. Also if you think it would be wrong to allow one to attempt to open a file using any type that can be transformed to a C string, then you might want to think about having a tiny wrapper trait with a more semantic name, like .to_path_str(), which would be generic over .to_c_str() and simply call that method on itself. Lots of little details.
Certainly, and for two reasons. First off, it's weakly typed. If, say, an integer type has a to_c_str() method, it does not mean that it should be possible to use it in File::open(). Secondly, the reliance of File::open() on to_c_str() is an implementation detail. At the end of the day, I can't say that having to write File::open(Path::new("foo.txt")) instead of File::open("foo.txt") is a huge burden. Actually, I'd even go the other way and distinguish between absolute (AbsPath) and relative (RelPath) versions of Path.
I have no opinion on whether "Path" is a good abstraction, but implicit conversions are bad bad bad.
Any pointers to where one would start to do raw mmap-ing in rust (say for implementing an os...)?
[1] http://static.rust-lang.org/doc/master/std/os/struct.MemoryM...
But, yes, I don't think there's been much effort put into streamlining it fully.
File.ReadAllLines(string) -> string[]
File.ReadAllText(string) -> string
File.ReadAllBytes(string) -> byte[]
Great abstractions for opening a file, reading the entire thing into memory, and closing it all in a single go. They will even figure out if the string you pass is a relative path or absolute path. Functions like these go a long way to making a language convenient to use.