3 ms·
Implicit conversions aren't really part of Rust's philosophy, but writing things once is. At the end of the day any file-opening function is gonna be reduced t
by kibwen 13y ago
Implicit conversions aren't really part of Rust's philosophy, but writing things once is.
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.
- mercurial 13y ago> 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. 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.