Skip to content

Commit 32bb15d

Browse files
committed
Auto merge of #55683 - sfackler:str-index, r=<try>
Use SliceIndex for str's Index/IndexMut impls This was missed by accident when the initial implementation landed! Closes #55603
2 parents e6c5cf9 + 55beec2 commit 32bb15d

File tree

1 file changed

+13
-177
lines changed

1 file changed

+13
-177
lines changed

Diff for: src/libcore/str/mod.rs

+13-177
Original file line numberDiff line numberDiff line change
@@ -1630,190 +1630,26 @@ mod traits {
16301630
}
16311631
}
16321632

1633-
/// Implements substring slicing with syntax `&self[begin .. end]`.
1634-
///
1635-
/// Returns a slice of the given string from the byte range
1636-
/// [`begin`..`end`).
1637-
///
1638-
/// This operation is `O(1)`.
1639-
///
1640-
/// # Panics
1641-
///
1642-
/// Panics if `begin` or `end` does not point to the starting
1643-
/// byte offset of a character (as defined by `is_char_boundary`).
1644-
/// Requires that `begin <= end` and `end <= len` where `len` is the
1645-
/// length of the string.
1646-
///
1647-
/// # Examples
1648-
///
1649-
/// ```
1650-
/// let s = "Löwe 老虎 Léopard";
1651-
/// assert_eq!(&s[0 .. 1], "L");
1652-
///
1653-
/// assert_eq!(&s[1 .. 9], "öwe 老");
1654-
///
1655-
/// // these will panic:
1656-
/// // byte 2 lies within `ö`:
1657-
/// // &s[2 ..3];
1658-
///
1659-
/// // byte 8 lies within `老`
1660-
/// // &s[1 .. 8];
1661-
///
1662-
/// // byte 100 is outside the string
1663-
/// // &s[3 .. 100];
1664-
/// ```
1665-
#[stable(feature = "rust1", since = "1.0.0")]
1666-
impl ops::Index<ops::Range<usize>> for str {
1667-
type Output = str;
1668-
#[inline]
1669-
fn index(&self, index: ops::Range<usize>) -> &str {
1670-
index.index(self)
1671-
}
1672-
}
1673-
1674-
/// Implements mutable substring slicing with syntax
1675-
/// `&mut self[begin .. end]`.
1676-
///
1677-
/// Returns a mutable slice of the given string from the byte range
1678-
/// [`begin`..`end`).
1679-
///
1680-
/// This operation is `O(1)`.
1681-
///
1682-
/// # Panics
1683-
///
1684-
/// Panics if `begin` or `end` does not point to the starting
1685-
/// byte offset of a character (as defined by `is_char_boundary`).
1686-
/// Requires that `begin <= end` and `end <= len` where `len` is the
1687-
/// length of the string.
1688-
#[stable(feature = "derefmut_for_string", since = "1.3.0")]
1689-
impl ops::IndexMut<ops::Range<usize>> for str {
1690-
#[inline]
1691-
fn index_mut(&mut self, index: ops::Range<usize>) -> &mut str {
1692-
index.index_mut(self)
1693-
}
1694-
}
1695-
1696-
/// Implements substring slicing with syntax `&self[.. end]`.
1697-
///
1698-
/// Returns a slice of the string from the beginning to byte offset
1699-
/// `end`.
1700-
///
1701-
/// Equivalent to `&self[0 .. end]`.
1702-
#[stable(feature = "rust1", since = "1.0.0")]
1703-
impl ops::Index<ops::RangeTo<usize>> for str {
1704-
type Output = str;
1705-
1706-
#[inline]
1707-
fn index(&self, index: ops::RangeTo<usize>) -> &str {
1708-
index.index(self)
1709-
}
1710-
}
1711-
1712-
/// Implements mutable substring slicing with syntax `&mut self[.. end]`.
1713-
///
1714-
/// Returns a mutable slice of the string from the beginning to byte offset
1715-
/// `end`.
1716-
///
1717-
/// Equivalent to `&mut self[0 .. end]`.
1718-
#[stable(feature = "derefmut_for_string", since = "1.3.0")]
1719-
impl ops::IndexMut<ops::RangeTo<usize>> for str {
1720-
#[inline]
1721-
fn index_mut(&mut self, index: ops::RangeTo<usize>) -> &mut str {
1722-
index.index_mut(self)
1723-
}
1724-
}
1725-
1726-
/// Implements substring slicing with syntax `&self[begin ..]`.
1727-
///
1728-
/// Returns a slice of the string from byte offset `begin`
1729-
/// to the end of the string.
1730-
///
1731-
/// Equivalent to `&self[begin .. len]`.
1732-
#[stable(feature = "rust1", since = "1.0.0")]
1733-
impl ops::Index<ops::RangeFrom<usize>> for str {
1734-
type Output = str;
1735-
1736-
#[inline]
1737-
fn index(&self, index: ops::RangeFrom<usize>) -> &str {
1738-
index.index(self)
1739-
}
1740-
}
1741-
1742-
/// Implements mutable substring slicing with syntax `&mut self[begin ..]`.
1743-
///
1744-
/// Returns a mutable slice of the string from byte offset `begin`
1745-
/// to the end of the string.
1746-
///
1747-
/// Equivalent to `&mut self[begin .. len]`.
1748-
#[stable(feature = "derefmut_for_string", since = "1.3.0")]
1749-
impl ops::IndexMut<ops::RangeFrom<usize>> for str {
1750-
#[inline]
1751-
fn index_mut(&mut self, index: ops::RangeFrom<usize>) -> &mut str {
1752-
index.index_mut(self)
1753-
}
1754-
}
1755-
1756-
/// Implements substring slicing with syntax `&self[..]`.
1757-
///
1758-
/// Returns a slice of the whole string. This operation can
1759-
/// never panic.
1760-
///
1761-
/// Equivalent to `&self[0 .. len]`.
1762-
#[stable(feature = "rust1", since = "1.0.0")]
1763-
impl ops::Index<ops::RangeFull> for str {
1764-
type Output = str;
1765-
1766-
#[inline]
1767-
fn index(&self, _index: ops::RangeFull) -> &str {
1768-
self
1769-
}
1770-
}
1771-
1772-
/// Implements mutable substring slicing with syntax `&mut self[..]`.
1773-
///
1774-
/// Returns a mutable slice of the whole string. This operation can
1775-
/// never panic.
1776-
///
1777-
/// Equivalent to `&mut self[0 .. len]`.
1778-
#[stable(feature = "derefmut_for_string", since = "1.3.0")]
1779-
impl ops::IndexMut<ops::RangeFull> for str {
1780-
#[inline]
1781-
fn index_mut(&mut self, _index: ops::RangeFull) -> &mut str {
1782-
self
1783-
}
1784-
}
1785-
1786-
#[stable(feature = "inclusive_range", since = "1.26.0")]
1787-
impl ops::Index<ops::RangeInclusive<usize>> for str {
1788-
type Output = str;
1789-
1790-
#[inline]
1791-
fn index(&self, index: ops::RangeInclusive<usize>) -> &str {
1792-
index.index(self)
1793-
}
1794-
}
1795-
1796-
#[stable(feature = "inclusive_range", since = "1.26.0")]
1797-
impl ops::Index<ops::RangeToInclusive<usize>> for str {
1798-
type Output = str;
1633+
#[stable(feature = "str_slice_index", since = "1.32.0")]
1634+
impl<T> ops::Index<T> for str
1635+
where
1636+
T: SliceIndex<str>
1637+
{
1638+
type Output = T::Output;
17991639

18001640
#[inline]
1801-
fn index(&self, index: ops::RangeToInclusive<usize>) -> &str {
1641+
fn index(&self, index: T) -> &T::Output {
18021642
index.index(self)
18031643
}
18041644
}
18051645

1806-
#[stable(feature = "inclusive_range", since = "1.26.0")]
1807-
impl ops::IndexMut<ops::RangeInclusive<usize>> for str {
1808-
#[inline]
1809-
fn index_mut(&mut self, index: ops::RangeInclusive<usize>) -> &mut str {
1810-
index.index_mut(self)
1811-
}
1812-
}
1813-
#[stable(feature = "inclusive_range", since = "1.26.0")]
1814-
impl ops::IndexMut<ops::RangeToInclusive<usize>> for str {
1646+
#[stable(feature = "str_slice_index", since = "1.32.0")]
1647+
impl<T> ops::IndexMut<T> for str
1648+
where
1649+
T: SliceIndex<str>
1650+
{
18151651
#[inline]
1816-
fn index_mut(&mut self, index: ops::RangeToInclusive<usize>) -> &mut str {
1652+
fn index_mut(&mut self, index: T) -> &mut T::Output {
18171653
index.index_mut(self)
18181654
}
18191655
}

0 commit comments

Comments
 (0)