-
Notifications
You must be signed in to change notification settings - Fork 13.3k
from_utf8 should take BytesContainer #17375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
The lifetimes here need more finesse, taking a |
Can you explain how this is different from the current from_str? |
I assume you mean "the current The current function has signature: fn from_utf8<'a>(x: &'a [u8]) -> Option<&'a str> while this effectively has signature fn from_utf8<'a, 'b: 'a>(x: &'a &'b [u8]) -> Option<&'a str> (for The extra reference and its shorter lifetime means the code would have to be written like the following, and it doesn't work: struct Foo {
name: Vec<u8>
}
impl Foo {
fn name_as_str<'a>(&'a self) -> Option<&'a str> {
str::from_utf8(&(self.as_slice()))
}
} since the (Written without lifetime elision, for preciseness.) DST may make this possible. (I.e. I'm just saying that a naive implementation wouldn't work, not that there isn't any implementation that works.) |
I think it would be more useful to add methods that allow you to freely cast between &[uN] and &[iN]. Maybe like this: #![feature(macro_rules)]
trait ImmutablePrimitiveSlice<'a, U, S> {
fn as_unsigned(self) -> &'a [U];
fn as_signed(self) -> &'a [S];
}
trait MutablePrimitiveSlice<'a, U, S>: ImmutablePrimitiveSlice<'a, U, S> {
fn as_unsigned_mut(self) -> &'a mut [U];
fn as_signed_mut(self) -> &'a mut [S];
}
macro_rules! prim_immut {
($u:ty, $s:ty, $t:ty) => {
impl<'a> ImmutablePrimitiveSlice<'a, $u, $s> for $t {
#[inline] fn as_unsigned(self) -> &'a [$u] { unsafe { std::mem::transmute(self) } }
#[inline] fn as_signed(self) -> &'a [$s] { unsafe { std::mem::transmute(self) } }
}
}
}
macro_rules! prim_mut {
($u:ty, $s:ty, $t:ty) => {
impl<'a> MutablePrimitiveSlice<'a, $u, $s> for $t {
#[inline]
fn as_unsigned_mut(self) -> &'a mut [$u] { unsafe { std::mem::transmute(self) } }
#[inline]
fn as_signed_mut(self) -> &'a mut [$s] { unsafe { std::mem::transmute(self) } }
}
}
}
macro_rules! prim {
($u:ty, $s:ty) => {
prim_immut!($u, $s, &'a [$u])
prim_immut!($u, $s, &'a [$s])
prim_immut!($u, $s, &'a mut [$u])
prim_immut!($u, $s, &'a mut [$s])
prim_mut!($u, $s, &'a mut [$u])
prim_mut!($u, $s, &'a mut [$s])
}
}
prim!(u8, i8)
prim!(u16, i16)
prim!(u32, i32)
prim!(u64, i64)
prim!(uint, int)
fn main() {
let x: &[i8] = [97, 98, 99];
println!("{}", std::str::from_utf8(x.as_unsigned()));
} |
This could benefit from the Coercible RFC. |
I believe the proposed design will eventually work fine with DST, and in fact I plan to change current uses of |
http://doc.rust-lang.org/nightly/std/str/fn.from_utf8.html still takes a |
Don't intern attribute inputs as their spans make them unique
std::str::from_utf8
takes&[u8]
which means that you can't create a string from a&[c_char]
in general.Instead, from_utf8 should look like this:
Then implement BytesContainer for &[i8].
The text was updated successfully, but these errors were encountered: