-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Closed
Milestone
Description
It's time to handle another one of the hardest things in computer science, how to name conversion functions. Rust supports essentially three ways of converting from one type to another. First there is doing a copy, such as these in the str
mod:
pub fn from_bytes(vv: &[u8]) -> ~str { ... }
pub trait StrSlice<'self> {
fn to_str(&self) -> ~str { ... }
fn to_owned(&self) -> ~str { ...}
}
Then we have some that can do a no-allocation moves, such as either
's:
pub fn to_result<T, U>(eith: Either<T, U>) -> Result<U, T> { ... }
Finally, we have ones that can get a no-copy reference, as in str
's:
pub fn from_bytes_with_null<'a>(vv: &'a [u8]) -> &'a str { ... }
pub trait StrSlice<'self> {
fn as_bytes(&self) -> &'self [u8]
}
While they share similar names, it's not consistent what exactly is happening in each case. I would like to standardize on a style so I can know how to name things for the standard library.
Some options that came up in IRC are:
as
for conversions, andto
for moves or copies. Simple, but then we end up with functions named liketo_str_consume
to distinguish between the two cases, which is a little ugly.as
for conversions,to
for moves, andinto
for copies. This is nice in that we try to push users to use our no-copy solutions, but this will make.to_str
a little more ugly.as
for conversions,to
for copies, andinto
for moves. This keeps.to_str
pretty, but people might tend to the copy functions instead of the moves.- Use a standard trait for conversions, as in RFC: Consider turning
as
into a user-implementable Cast trait #7080. This won't help when we have options for how to convert. For example,from_bytes_slice<'a>(vector: &'a [u8]) -> &'a str
which handles byte slices that don't end with a null character, orstr::from_bytes_with_null<'a>(vv: &'a [u8]) -> &'a str
which does.
Anyone have an opinion on what we should use?