@@ -726,7 +726,7 @@ pub trait Read {
726
726
/// # fn foo() -> io::Result<()> {
727
727
/// let mut f = try!(File::open("foo.txt"));
728
728
///
729
- /// for c in f.chars () {
729
+ /// for c in f.utf8_chars () {
730
730
/// println!("{}", c.unwrap());
731
731
/// }
732
732
/// # Ok(())
@@ -736,8 +736,15 @@ pub trait Read {
736
736
of where errors happen is currently \
737
737
unclear and may change",
738
738
issue = "27802" ) ]
739
- fn chars ( self ) -> Chars < Self > where Self : Sized {
740
- Chars { inner : self , buffer : None }
739
+ fn utf8_chars ( self ) -> Utf8Chars < Self > where Self : Sized {
740
+ Utf8Chars { inner : self , buffer : None }
741
+ }
742
+
743
+ /// Former name of the `utf8_chars` method.
744
+ #[ rustc_deprecated( since = "1.10.0" , reason = "renamed to `utf8_chars`" ) ]
745
+ #[ unstable( feature = "io" , reason = "renamed while unstable" , issue = "27802" ) ]
746
+ fn chars ( self ) -> Utf8Chars < Self > where Self : Sized {
747
+ self . utf8_chars ( )
741
748
}
742
749
743
750
/// Creates an adaptor which will chain this stream with another.
@@ -1547,23 +1554,23 @@ impl<R: Read> Iterator for Bytes<R> {
1547
1554
1548
1555
/// An iterator over the `char`s of a reader.
1549
1556
///
1550
- /// This struct is generally created by calling [`chars ()`][chars ] on a reader.
1551
- /// Please see the documentation of `chars ()` for more details.
1557
+ /// This struct is generally created by calling [`utf8_chars ()`][utf8_chars ] on a reader.
1558
+ /// Please see the documentation of `utf8_chars ()` for more details.
1552
1559
///
1553
- /// [chars ]: trait.Read.html#method.chars
1554
- #[ unstable( feature = "io" , reason = "awaiting stability of Read::chars " ,
1560
+ /// [utf8_chars ]: trait.Read.html#method.utf8_chars
1561
+ #[ unstable( feature = "io" , reason = "awaiting stability of Read::utf8_chars " ,
1555
1562
issue = "27802" ) ]
1556
- pub struct Chars < R > {
1563
+ pub struct Utf8Chars < R > {
1557
1564
inner : R ,
1558
1565
buffer : Option < u8 > ,
1559
1566
}
1560
1567
1561
- /// An enumeration of possible errors that can be generated from the `Chars `
1568
+ /// An enumeration of possible errors that can be generated from the `Utf8Chars `
1562
1569
/// adapter.
1563
1570
#[ derive( Debug ) ]
1564
- #[ unstable( feature = "io" , reason = "awaiting stability of Read::chars " ,
1571
+ #[ unstable( feature = "io" , reason = "awaiting stability of Read::utf8_chars " ,
1565
1572
issue = "27802" ) ]
1566
- pub enum CharsError {
1573
+ pub enum Utf8CharsError {
1567
1574
/// Variant representing that the underlying stream was read successfully
1568
1575
/// but contains a byte sequence ill-formed in UTF-8.
1569
1576
InvalidUtf8 ,
@@ -1576,12 +1583,12 @@ pub enum CharsError {
1576
1583
Io ( Error ) ,
1577
1584
}
1578
1585
1579
- #[ unstable( feature = "io" , reason = "awaiting stability of Read::chars " ,
1586
+ #[ unstable( feature = "io" , reason = "awaiting stability of Read::utf8_chars " ,
1580
1587
issue = "27802" ) ]
1581
- impl < R : Read > Iterator for Chars < R > {
1582
- type Item = result:: Result < char , CharsError > ;
1588
+ impl < R : Read > Iterator for Utf8Chars < R > {
1589
+ type Item = result:: Result < char , Utf8CharsError > ;
1583
1590
1584
- fn next ( & mut self ) -> Option < result:: Result < char , CharsError > > {
1591
+ fn next ( & mut self ) -> Option < result:: Result < char , Utf8CharsError > > {
1585
1592
let mut buf = [ 0 ] ;
1586
1593
macro_rules! read_byte {
1587
1594
( EOF => $on_eof: expr) => {
@@ -1591,7 +1598,7 @@ impl<R: Read> Iterator for Chars<R> {
1591
1598
Ok ( 0 ) => $on_eof,
1592
1599
Ok ( ..) => break ,
1593
1600
Err ( ref e) if e. kind( ) == ErrorKind :: Interrupted => { }
1594
- Err ( e) => return Some ( Err ( CharsError :: Io ( e) ) ) ,
1601
+ Err ( e) => return Some ( Err ( Utf8CharsError :: Io ( e) ) ) ,
1595
1602
}
1596
1603
}
1597
1604
buf[ 0 ]
@@ -1607,11 +1614,11 @@ impl<R: Read> Iterator for Chars<R> {
1607
1614
macro_rules! continuation_byte {
1608
1615
( $range: pat) => {
1609
1616
{
1610
- match read_byte!( EOF => return Some ( Err ( CharsError :: IncompleteUtf8 ) ) ) {
1617
+ match read_byte!( EOF => return Some ( Err ( Utf8CharsError :: IncompleteUtf8 ) ) ) {
1611
1618
byte @ $range => ( byte & 0b0011_1111 ) as u32 ,
1612
1619
byte => {
1613
1620
self . buffer = Some ( byte) ;
1614
- return Some ( Err ( CharsError :: InvalidUtf8 ) )
1621
+ return Some ( Err ( Utf8CharsError :: InvalidUtf8 ) )
1615
1622
}
1616
1623
}
1617
1624
}
@@ -1647,46 +1654,46 @@ impl<R: Read> Iterator for Chars<R> {
1647
1654
let fourth = continuation_byte ! ( 0x80 ...0xBF ) ;
1648
1655
( ( first & 0b0000_0111 ) as u32 ) << 18 | second << 12 | third << 6 | fourth
1649
1656
}
1650
- _ => return Some ( Err ( CharsError :: InvalidUtf8 ) )
1657
+ _ => return Some ( Err ( Utf8CharsError :: InvalidUtf8 ) )
1651
1658
} ;
1652
1659
unsafe {
1653
1660
Some ( Ok ( char:: from_u32_unchecked ( code_point) ) )
1654
1661
}
1655
1662
}
1656
1663
}
1657
1664
1658
- #[ unstable( feature = "io" , reason = "awaiting stability of Read::chars " ,
1665
+ #[ unstable( feature = "io" , reason = "awaiting stability of Read::utf8_chars " ,
1659
1666
issue = "27802" ) ]
1660
- impl std_error:: Error for CharsError {
1667
+ impl std_error:: Error for Utf8CharsError {
1661
1668
fn description ( & self ) -> & str {
1662
1669
match * self {
1663
- CharsError :: InvalidUtf8 => "invalid UTF-8 byte sequence" ,
1664
- CharsError :: IncompleteUtf8 => {
1670
+ Utf8CharsError :: InvalidUtf8 => "invalid UTF-8 byte sequence" ,
1671
+ Utf8CharsError :: IncompleteUtf8 => {
1665
1672
"stream ended in the middle of an UTF-8 byte sequence"
1666
1673
}
1667
- CharsError :: Io ( ref e) => std_error:: Error :: description ( e) ,
1674
+ Utf8CharsError :: Io ( ref e) => std_error:: Error :: description ( e) ,
1668
1675
}
1669
1676
}
1670
1677
fn cause ( & self ) -> Option < & std_error:: Error > {
1671
1678
match * self {
1672
- CharsError :: InvalidUtf8 | CharsError :: IncompleteUtf8 => None ,
1673
- CharsError :: Io ( ref e) => e. cause ( ) ,
1679
+ Utf8CharsError :: InvalidUtf8 | Utf8CharsError :: IncompleteUtf8 => None ,
1680
+ Utf8CharsError :: Io ( ref e) => e. cause ( ) ,
1674
1681
}
1675
1682
}
1676
1683
}
1677
1684
1678
- #[ unstable( feature = "io" , reason = "awaiting stability of Read::chars " ,
1685
+ #[ unstable( feature = "io" , reason = "awaiting stability of Read::utf8_chars " ,
1679
1686
issue = "27802" ) ]
1680
- impl fmt:: Display for CharsError {
1687
+ impl fmt:: Display for Utf8CharsError {
1681
1688
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
1682
1689
match * self {
1683
- CharsError :: InvalidUtf8 => {
1690
+ Utf8CharsError :: InvalidUtf8 => {
1684
1691
"invalid UTF-8 byte sequence" . fmt ( f)
1685
1692
}
1686
- CharsError :: IncompleteUtf8 => {
1693
+ Utf8CharsError :: IncompleteUtf8 => {
1687
1694
"stream ended in the middle of an UTF-8 byte sequence" . fmt ( f)
1688
1695
}
1689
- CharsError :: Io ( ref e) => e. fmt ( f) ,
1696
+ Utf8CharsError :: Io ( ref e) => e. fmt ( f) ,
1690
1697
}
1691
1698
}
1692
1699
}
@@ -1761,23 +1768,24 @@ mod tests {
1761
1768
use prelude:: v1:: * ;
1762
1769
use io:: prelude:: * ;
1763
1770
use io;
1764
- use super :: CharsError ;
1771
+ use super :: Utf8CharsError ;
1765
1772
use super :: Cursor ;
1766
1773
use test;
1767
1774
use super :: repeat;
1768
1775
1769
1776
fn chars_lossy ( bytes : & [ u8 ] ) -> String {
1770
1777
// Follow Unicode Standard §5.22 "Best Practice for U+FFFD Substitution"
1771
1778
// http://www.unicode.org/versions/Unicode8.0.0/ch05.pdf#G40630
1772
- Cursor :: new ( bytes) . chars ( ) . map ( |result| match result {
1779
+ Cursor :: new ( bytes) . utf8_chars ( ) . map ( |result| match result {
1773
1780
Ok ( c) => c,
1774
- Err ( CharsError :: InvalidUtf8 ) | Err ( CharsError :: IncompleteUtf8 ) => '\u{FFFD}' ,
1775
- Err ( CharsError :: Io ( e) ) => panic ! ( "{}" , e) ,
1781
+ Err ( Utf8CharsError :: InvalidUtf8 ) |
1782
+ Err ( Utf8CharsError :: IncompleteUtf8 ) => '\u{FFFD}' ,
1783
+ Err ( Utf8CharsError :: Io ( e) ) => panic ! ( "{}" , e) ,
1776
1784
} ) . collect ( )
1777
1785
}
1778
1786
1779
1787
#[ test]
1780
- fn chars ( ) {
1788
+ fn utf8_chars ( ) {
1781
1789
assert_eq ! ( chars_lossy( b"\xf0 \x9f abc" ) , "�abc" ) ;
1782
1790
assert_eq ! ( chars_lossy( b"\xed \xa0 \x80 a" ) , "���a" ) ;
1783
1791
assert_eq ! ( chars_lossy( b"\xed \xa0 a" ) , "��a" ) ;
0 commit comments