3 ms·
Huh. Where is the "indirect promise" ? Is there a (conventionally hinted as "free", so it's not good if there's one which isn't very cheap) as function? Like as
by tialaramex 3y ago
Huh. Where is the "indirect promise" ? Is there a (conventionally hinted as "free", so it's not good if there's one which isn't very cheap) as function? Like as_os_string or something?
- LegionMammal978 3y agoIt's because str implements AsRef<OsStr> [0]. The function signature promises that whenever you have a borrowed &'a str, the standard library can give you a borrowed &'a OsStr with the same data. Since references can't have destructors (they don't own the data like an OsString does), it means that the standard library can't give you a newly-allocated string without leaking it. Since obviously it isn't going to do that, the &OsStr must instead just act as a view into the underlying &str. And the conversion can't enforce any extra restrictions on the input string without breaking backward compatibility. The overall effect is that whatever format OsStr uses, it has to be a superset of UTF-8. [0] https://doc.rust-lang.org/1.77.2/std/ffi/struct.OsStr.html#impl-AsRef%3COsStr%3E-for-str https://doc.rust-lang.org/1.77.2/std/ffi/struct.OsStr.html#i...
- tialaramex 3y agoAh, an AsRef<OsStr>, yep that would do it. Thanks.