Skip to content

Commit fd302a9

Browse files
committed
Auto merge of #27808 - SimonSapin:utf16decoder, r=alexcrichton
* Rename `Utf16Items` to `Utf16Decoder`. "Items" is meaningless. * Generalize it to any `u16` iterator, not just `[u16].iter()` * Make it yield `Result` instead of a custom `Utf16Item` enum that was isomorphic to `Result`. This enable using the `FromIterator for Result` impl. * Replace `Utf16Item::to_char_lossy` with a `Utf16Decoder::lossy` iterator adaptor. This is a [breaking change], but only for users of the unstable `rustc_unicode` crate. I’d like this functionality to be stabilized and re-exported in `std` eventually, as the "low-level equivalent" of `String::from_utf16` and `String::from_utf16_lossy` like #27784 is the low-level equivalent of #27714. CC @aturon, @alexcrichton
2 parents 80b971a + 6174b8d commit fd302a9

File tree

16 files changed

+293
-171
lines changed

16 files changed

+293
-171
lines changed

src/liballoc/arc.rs

+5
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ use boxed::Box;
7373

7474
use core::sync::atomic;
7575
use core::sync::atomic::Ordering::{Relaxed, Release, Acquire, SeqCst};
76+
use core::borrow;
7677
use core::fmt;
7778
use core::cmp::Ordering;
7879
use core::mem::{align_of_val, size_of_val};
@@ -1109,3 +1110,7 @@ mod tests {
11091110
assert!(y.upgrade().is_none());
11101111
}
11111112
}
1113+
1114+
impl<T: ?Sized> borrow::Borrow<T> for Arc<T> {
1115+
fn borrow(&self) -> &T { &**self }
1116+
}

src/liballoc/boxed.rs

+8
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ use heap;
5757
use raw_vec::RawVec;
5858

5959
use core::any::Any;
60+
use core::borrow;
6061
use core::cmp::Ordering;
6162
use core::fmt;
6263
use core::hash::{self, Hash};
@@ -562,3 +563,10 @@ impl<T: Clone> Clone for Box<[T]> {
562563
}
563564
}
564565

566+
impl<T: ?Sized> borrow::Borrow<T> for Box<T> {
567+
fn borrow(&self) -> &T { &**self }
568+
}
569+
570+
impl<T: ?Sized> borrow::BorrowMut<T> for Box<T> {
571+
fn borrow_mut(&mut self) -> &mut T { &mut **self }
572+
}

src/liballoc/rc.rs

+5
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ use boxed::Box;
158158
#[cfg(test)]
159159
use std::boxed::Box;
160160

161+
use core::borrow;
161162
use core::cell::Cell;
162163
use core::cmp::Ordering;
163164
use core::fmt;
@@ -1091,3 +1092,7 @@ mod tests {
10911092
assert_eq!(foo, foo.clone());
10921093
}
10931094
}
1095+
1096+
impl<T: ?Sized> borrow::Borrow<T> for Rc<T> {
1097+
fn borrow(&self) -> &T { &**self }
1098+
}

src/libcollections/borrow.rs

+1-110
Original file line numberDiff line numberDiff line change
@@ -21,119 +21,10 @@ use core::ops::Deref;
2121
use core::option::Option;
2222

2323
use fmt;
24-
use alloc::{boxed, rc, arc};
2524

2625
use self::Cow::*;
2726

28-
/// A trait for borrowing data.
29-
///
30-
/// In general, there may be several ways to "borrow" a piece of data. The
31-
/// typical ways of borrowing a type `T` are `&T` (a shared borrow) and `&mut T`
32-
/// (a mutable borrow). But types like `Vec<T>` provide additional kinds of
33-
/// borrows: the borrowed slices `&[T]` and `&mut [T]`.
34-
///
35-
/// When writing generic code, it is often desirable to abstract over all ways
36-
/// of borrowing data from a given type. That is the role of the `Borrow`
37-
/// trait: if `T: Borrow<U>`, then `&U` can be borrowed from `&T`. A given
38-
/// type can be borrowed as multiple different types. In particular, `Vec<T>:
39-
/// Borrow<Vec<T>>` and `Vec<T>: Borrow<[T]>`.
40-
///
41-
/// If you are implementing `Borrow` and both `Self` and `Borrowed` implement
42-
/// `Hash`, `Eq`, and/or `Ord`, they must produce the same result.
43-
///
44-
/// `Borrow` is very similar to, but different than, `AsRef`. See
45-
/// [the book][book] for more.
46-
///
47-
/// [book]: ../../book/borrow-and-asref.html
48-
#[stable(feature = "rust1", since = "1.0.0")]
49-
pub trait Borrow<Borrowed: ?Sized> {
50-
/// Immutably borrows from an owned value.
51-
///
52-
/// # Examples
53-
///
54-
/// ```
55-
/// use std::borrow::Borrow;
56-
///
57-
/// fn check<T: Borrow<str>>(s: T) {
58-
/// assert_eq!("Hello", s.borrow());
59-
/// }
60-
///
61-
/// let s = "Hello".to_string();
62-
///
63-
/// check(s);
64-
///
65-
/// let s = "Hello";
66-
///
67-
/// check(s);
68-
/// ```
69-
#[stable(feature = "rust1", since = "1.0.0")]
70-
fn borrow(&self) -> &Borrowed;
71-
}
72-
73-
/// A trait for mutably borrowing data.
74-
///
75-
/// Similar to `Borrow`, but for mutable borrows.
76-
#[stable(feature = "rust1", since = "1.0.0")]
77-
pub trait BorrowMut<Borrowed: ?Sized> : Borrow<Borrowed> {
78-
/// Mutably borrows from an owned value.
79-
///
80-
/// # Examples
81-
///
82-
/// ```
83-
/// use std::borrow::BorrowMut;
84-
///
85-
/// fn check<T: BorrowMut<[i32]>>(mut v: T) {
86-
/// assert_eq!(&mut [1, 2, 3], v.borrow_mut());
87-
/// }
88-
///
89-
/// let v = vec![1, 2, 3];
90-
///
91-
/// check(v);
92-
/// ```
93-
#[stable(feature = "rust1", since = "1.0.0")]
94-
fn borrow_mut(&mut self) -> &mut Borrowed;
95-
}
96-
97-
#[stable(feature = "rust1", since = "1.0.0")]
98-
impl<T: ?Sized> Borrow<T> for T {
99-
fn borrow(&self) -> &T { self }
100-
}
101-
102-
#[stable(feature = "rust1", since = "1.0.0")]
103-
impl<T: ?Sized> BorrowMut<T> for T {
104-
fn borrow_mut(&mut self) -> &mut T { self }
105-
}
106-
107-
#[stable(feature = "rust1", since = "1.0.0")]
108-
impl<'a, T: ?Sized> Borrow<T> for &'a T {
109-
fn borrow(&self) -> &T { &**self }
110-
}
111-
112-
#[stable(feature = "rust1", since = "1.0.0")]
113-
impl<'a, T: ?Sized> Borrow<T> for &'a mut T {
114-
fn borrow(&self) -> &T { &**self }
115-
}
116-
117-
#[stable(feature = "rust1", since = "1.0.0")]
118-
impl<'a, T: ?Sized> BorrowMut<T> for &'a mut T {
119-
fn borrow_mut(&mut self) -> &mut T { &mut **self }
120-
}
121-
122-
impl<T: ?Sized> Borrow<T> for boxed::Box<T> {
123-
fn borrow(&self) -> &T { &**self }
124-
}
125-
126-
impl<T: ?Sized> BorrowMut<T> for boxed::Box<T> {
127-
fn borrow_mut(&mut self) -> &mut T { &mut **self }
128-
}
129-
130-
impl<T: ?Sized> Borrow<T> for rc::Rc<T> {
131-
fn borrow(&self) -> &T { &**self }
132-
}
133-
134-
impl<T: ?Sized> Borrow<T> for arc::Arc<T> {
135-
fn borrow(&self) -> &T { &**self }
136-
}
27+
pub use core::borrow::{Borrow, BorrowMut};
13728

13829
#[stable(feature = "rust1", since = "1.0.0")]
13930
impl<'a, B: ?Sized> Borrow<B> for Cow<'a, B> where B: ToOwned, <B as ToOwned>::Owned: 'a {

src/libcollections/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
#![feature(unicode)]
5757
#![feature(unique)]
5858
#![feature(unsafe_no_drop_flag, filling_drop)]
59+
#![feature(decode_utf16)]
5960
#![feature(utf8_error)]
6061
#![cfg_attr(test, feature(rand, test))]
6162

src/libcollections/string.rs

+3-10
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ use core::ops::{self, Deref, Add, Index};
2020
use core::ptr;
2121
use core::slice;
2222
use core::str::pattern::Pattern;
23+
use rustc_unicode::char::{decode_utf16, REPLACEMENT_CHARACTER};
2324
use rustc_unicode::str as unicode_str;
24-
use rustc_unicode::str::Utf16Item;
2525

2626
use borrow::{Cow, IntoCow};
2727
use range::RangeArgument;
@@ -267,14 +267,7 @@ impl String {
267267
/// ```
268268
#[stable(feature = "rust1", since = "1.0.0")]
269269
pub fn from_utf16(v: &[u16]) -> Result<String, FromUtf16Error> {
270-
let mut s = String::with_capacity(v.len());
271-
for c in unicode_str::utf16_items(v) {
272-
match c {
273-
Utf16Item::ScalarValue(c) => s.push(c),
274-
Utf16Item::LoneSurrogate(_) => return Err(FromUtf16Error(())),
275-
}
276-
}
277-
Ok(s)
270+
decode_utf16(v.iter().cloned()).collect::<Result<_, _>>().map_err(|_| FromUtf16Error(()))
278271
}
279272

280273
/// Decode a UTF-16 encoded vector `v` into a string, replacing
@@ -294,7 +287,7 @@ impl String {
294287
#[inline]
295288
#[stable(feature = "rust1", since = "1.0.0")]
296289
pub fn from_utf16_lossy(v: &[u16]) -> String {
297-
unicode_str::utf16_items(v).map(|c| c.to_char_lossy()).collect()
290+
decode_utf16(v.iter().cloned()).map(|r| r.unwrap_or(REPLACEMENT_CHARACTER)).collect()
298291
}
299292

300293
/// Creates a new `String` from a length, capacity, and pointer.

src/libcore/borrow.rs

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
//! A module for working with borrowed data.
12+
13+
#![stable(feature = "rust1", since = "1.0.0")]
14+
15+
use marker::Sized;
16+
17+
/// A trait for borrowing data.
18+
///
19+
/// In general, there may be several ways to "borrow" a piece of data. The
20+
/// typical ways of borrowing a type `T` are `&T` (a shared borrow) and `&mut T`
21+
/// (a mutable borrow). But types like `Vec<T>` provide additional kinds of
22+
/// borrows: the borrowed slices `&[T]` and `&mut [T]`.
23+
///
24+
/// When writing generic code, it is often desirable to abstract over all ways
25+
/// of borrowing data from a given type. That is the role of the `Borrow`
26+
/// trait: if `T: Borrow<U>`, then `&U` can be borrowed from `&T`. A given
27+
/// type can be borrowed as multiple different types. In particular, `Vec<T>:
28+
/// Borrow<Vec<T>>` and `Vec<T>: Borrow<[T]>`.
29+
///
30+
/// If you are implementing `Borrow` and both `Self` and `Borrowed` implement
31+
/// `Hash`, `Eq`, and/or `Ord`, they must produce the same result.
32+
///
33+
/// `Borrow` is very similar to, but different than, `AsRef`. See
34+
/// [the book][book] for more.
35+
///
36+
/// [book]: ../../book/borrow-and-asref.html
37+
#[stable(feature = "rust1", since = "1.0.0")]
38+
pub trait Borrow<Borrowed: ?Sized> {
39+
/// Immutably borrows from an owned value.
40+
///
41+
/// # Examples
42+
///
43+
/// ```
44+
/// use std::borrow::Borrow;
45+
///
46+
/// fn check<T: Borrow<str>>(s: T) {
47+
/// assert_eq!("Hello", s.borrow());
48+
/// }
49+
///
50+
/// let s = "Hello".to_string();
51+
///
52+
/// check(s);
53+
///
54+
/// let s = "Hello";
55+
///
56+
/// check(s);
57+
/// ```
58+
#[stable(feature = "rust1", since = "1.0.0")]
59+
fn borrow(&self) -> &Borrowed;
60+
}
61+
62+
/// A trait for mutably borrowing data.
63+
///
64+
/// Similar to `Borrow`, but for mutable borrows.
65+
#[stable(feature = "rust1", since = "1.0.0")]
66+
pub trait BorrowMut<Borrowed: ?Sized> : Borrow<Borrowed> {
67+
/// Mutably borrows from an owned value.
68+
///
69+
/// # Examples
70+
///
71+
/// ```
72+
/// use std::borrow::BorrowMut;
73+
///
74+
/// fn check<T: BorrowMut<[i32]>>(mut v: T) {
75+
/// assert_eq!(&mut [1, 2, 3], v.borrow_mut());
76+
/// }
77+
///
78+
/// let v = vec![1, 2, 3];
79+
///
80+
/// check(v);
81+
/// ```
82+
#[stable(feature = "rust1", since = "1.0.0")]
83+
fn borrow_mut(&mut self) -> &mut Borrowed;
84+
}
85+
86+
#[stable(feature = "rust1", since = "1.0.0")]
87+
impl<T: ?Sized> Borrow<T> for T {
88+
fn borrow(&self) -> &T { self }
89+
}
90+
91+
#[stable(feature = "rust1", since = "1.0.0")]
92+
impl<T: ?Sized> BorrowMut<T> for T {
93+
fn borrow_mut(&mut self) -> &mut T { self }
94+
}
95+
96+
#[stable(feature = "rust1", since = "1.0.0")]
97+
impl<'a, T: ?Sized> Borrow<T> for &'a T {
98+
fn borrow(&self) -> &T { &**self }
99+
}
100+
101+
#[stable(feature = "rust1", since = "1.0.0")]
102+
impl<'a, T: ?Sized> Borrow<T> for &'a mut T {
103+
fn borrow(&self) -> &T { &**self }
104+
}
105+
106+
#[stable(feature = "rust1", since = "1.0.0")]
107+
impl<'a, T: ?Sized> BorrowMut<T> for &'a mut T {
108+
fn borrow_mut(&mut self) -> &mut T { &mut **self }
109+
}

src/libcore/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ pub mod cmp;
139139
pub mod clone;
140140
pub mod default;
141141
pub mod convert;
142+
pub mod borrow;
142143

143144
/* Core types and methods on primitives */
144145

src/libcoretest/char.rs

+9
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,12 @@ fn test_len_utf16() {
207207
assert!('\u{a66e}'.len_utf16() == 1);
208208
assert!('\u{1f4a9}'.len_utf16() == 2);
209209
}
210+
211+
#[test]
212+
fn test_decode_utf16() {
213+
fn check(s: &[u16], expected: &[Result<char, u16>]) {
214+
assert_eq!(::std::char::decode_utf16(s.iter().cloned()).collect::<Vec<_>>(), expected);
215+
}
216+
check(&[0xD800, 0x41, 0x42], &[Err(0xD800), Ok('A'), Ok('B')]);
217+
check(&[0xD800, 0], &[Err(0xD800), Ok('\0')]);
218+
}

src/libcoretest/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#![feature(float_from_str_radix)]
2020
#![feature(flt2dec)]
2121
#![feature(dec2flt)]
22+
#![feature(decode_utf16)]
2223
#![feature(fmt_radix)]
2324
#![feature(iter_arith)]
2425
#![feature(iter_arith)]

0 commit comments

Comments
 (0)