Skip to content

Commit 0181fa5

Browse files
committed
Move non-allocating [u8] inherent methods to libcore
Fixes rust-lang#45803
1 parent fb105d2 commit 0181fa5

File tree

7 files changed

+101
-56
lines changed

7 files changed

+101
-56
lines changed

src/liballoc/slice.rs

+4-54
Original file line numberDiff line numberDiff line change
@@ -1793,16 +1793,10 @@ impl<T> [T] {
17931793
}
17941794
}
17951795

1796-
#[lang = "slice_u8"]
1796+
#[cfg_attr(stage0, lang = "slice_u8")]
1797+
#[cfg_attr(not(stage0), lang = "slice_u8_alloc")]
17971798
#[cfg(not(test))]
17981799
impl [u8] {
1799-
/// Checks if all bytes in this slice are within the ASCII range.
1800-
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
1801-
#[inline]
1802-
pub fn is_ascii(&self) -> bool {
1803-
self.iter().all(|b| b.is_ascii())
1804-
}
1805-
18061800
/// Returns a vector containing a copy of this slice where each byte
18071801
/// is mapped to its ASCII upper case equivalent.
18081802
///
@@ -1837,52 +1831,8 @@ impl [u8] {
18371831
me
18381832
}
18391833

1840-
/// Checks that two slices are an ASCII case-insensitive match.
1841-
///
1842-
/// Same as `to_ascii_lowercase(a) == to_ascii_lowercase(b)`,
1843-
/// but without allocating and copying temporaries.
1844-
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
1845-
#[inline]
1846-
pub fn eq_ignore_ascii_case(&self, other: &[u8]) -> bool {
1847-
self.len() == other.len() &&
1848-
self.iter().zip(other).all(|(a, b)| {
1849-
a.eq_ignore_ascii_case(b)
1850-
})
1851-
}
1852-
1853-
/// Converts this slice to its ASCII upper case equivalent in-place.
1854-
///
1855-
/// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
1856-
/// but non-ASCII letters are unchanged.
1857-
///
1858-
/// To return a new uppercased value without modifying the existing one, use
1859-
/// [`to_ascii_uppercase`].
1860-
///
1861-
/// [`to_ascii_uppercase`]: #method.to_ascii_uppercase
1862-
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
1863-
#[inline]
1864-
pub fn make_ascii_uppercase(&mut self) {
1865-
for byte in self {
1866-
byte.make_ascii_uppercase();
1867-
}
1868-
}
1869-
1870-
/// Converts this slice to its ASCII lower case equivalent in-place.
1871-
///
1872-
/// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
1873-
/// but non-ASCII letters are unchanged.
1874-
///
1875-
/// To return a new lowercased value without modifying the existing one, use
1876-
/// [`to_ascii_lowercase`].
1877-
///
1878-
/// [`to_ascii_lowercase`]: #method.to_ascii_lowercase
1879-
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
1880-
#[inline]
1881-
pub fn make_ascii_lowercase(&mut self) {
1882-
for byte in self {
1883-
byte.make_ascii_lowercase();
1884-
}
1885-
}
1834+
#[cfg(stage0)]
1835+
slice_u8_core_methods!();
18861836
}
18871837

18881838
////////////////////////////////////////////////////////////////////////////////

src/libcore/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
#![feature(rustc_attrs)]
9393
#![feature(rustc_const_unstable)]
9494
#![feature(simd_ffi)]
95+
#![feature(core_slice_ext)]
9596
#![feature(specialization)]
9697
#![feature(staged_api)]
9798
#![feature(stmt_expr_attributes)]

src/libcore/slice/mod.rs

+65
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,71 @@ impl<T> SliceExt for [T] {
755755
}
756756
}
757757

758+
#[cfg_attr(stage0, macro_export)]
759+
#[unstable(feature = "core_slice_ext", issue = "32110")]
760+
macro_rules! slice_u8_core_methods { () => {
761+
/// Checks if all bytes in this slice are within the ASCII range.
762+
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
763+
#[inline]
764+
pub fn is_ascii(&self) -> bool {
765+
self.iter().all(|b| b.is_ascii())
766+
}
767+
768+
/// Checks that two slices are an ASCII case-insensitive match.
769+
///
770+
/// Same as `to_ascii_lowercase(a) == to_ascii_lowercase(b)`,
771+
/// but without allocating and copying temporaries.
772+
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
773+
#[inline]
774+
pub fn eq_ignore_ascii_case(&self, other: &[u8]) -> bool {
775+
self.len() == other.len() &&
776+
self.iter().zip(other).all(|(a, b)| {
777+
a.eq_ignore_ascii_case(b)
778+
})
779+
}
780+
781+
/// Converts this slice to its ASCII upper case equivalent in-place.
782+
///
783+
/// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
784+
/// but non-ASCII letters are unchanged.
785+
///
786+
/// To return a new uppercased value without modifying the existing one, use
787+
/// [`to_ascii_uppercase`].
788+
///
789+
/// [`to_ascii_uppercase`]: #method.to_ascii_uppercase
790+
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
791+
#[inline]
792+
pub fn make_ascii_uppercase(&mut self) {
793+
for byte in self {
794+
byte.make_ascii_uppercase();
795+
}
796+
}
797+
798+
/// Converts this slice to its ASCII lower case equivalent in-place.
799+
///
800+
/// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
801+
/// but non-ASCII letters are unchanged.
802+
///
803+
/// To return a new lowercased value without modifying the existing one, use
804+
/// [`to_ascii_lowercase`].
805+
///
806+
/// [`to_ascii_lowercase`]: #method.to_ascii_lowercase
807+
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
808+
#[inline]
809+
pub fn make_ascii_lowercase(&mut self) {
810+
for byte in self {
811+
byte.make_ascii_lowercase();
812+
}
813+
}
814+
}}
815+
816+
#[lang = "slice_u8"]
817+
#[cfg(not(test))]
818+
#[cfg(not(stage0))]
819+
impl [u8] {
820+
slice_u8_core_methods!();
821+
}
822+
758823
#[stable(feature = "rust1", since = "1.0.0")]
759824
#[rustc_on_unimplemented = "slice indices are of type `usize` or ranges of `usize`"]
760825
impl<T, I> ops::Index<I> for [T]

src/librustc/middle/lang_items.rs

+1
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ language_item_table! {
214214
StrImplItem, "str", str_impl;
215215
SliceImplItem, "slice", slice_impl;
216216
SliceU8ImplItem, "slice_u8", slice_u8_impl;
217+
SliceU8AllocImplItem, "slice_u8_alloc", slice_u8_alloc_impl;
217218
ConstPtrImplItem, "const_ptr", const_ptr_impl;
218219
MutPtrImplItem, "mut_ptr", mut_ptr_impl;
219220
I8ImplItem, "i8", i8_impl;

src/librustc_typeck/check/method/probe.rs

+3
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,9 @@ impl<'a, 'gcx, 'tcx> ProbeContext<'a, 'gcx, 'tcx> {
478478

479479
let lang_def_id = lang_items.slice_u8_impl();
480480
self.assemble_inherent_impl_for_primitive(lang_def_id);
481+
482+
let lang_def_id = lang_items.slice_u8_alloc_impl();
483+
self.assemble_inherent_impl_for_primitive(lang_def_id);
481484
}
482485
ty::TyRawPtr(ty::TypeAndMut { ty: _, mutbl: hir::MutImmutable }) => {
483486
let lang_def_id = lang_items.const_ptr_impl();

src/librustc_typeck/coherence/inherent_impls.rs

+26-2
Original file line numberDiff line numberDiff line change
@@ -114,139 +114,159 @@ impl<'a, 'tcx, 'v> ItemLikeVisitor<'v> for InherentCollect<'a, 'tcx> {
114114
ty::TyChar => {
115115
self.check_primitive_impl(def_id,
116116
lang_items.char_impl(),
117+
None,
117118
"char",
118119
"char",
119120
item.span);
120121
}
121122
ty::TyStr => {
122123
self.check_primitive_impl(def_id,
123124
lang_items.str_impl(),
125+
None,
124126
"str",
125127
"str",
126128
item.span);
127129
}
128130
ty::TySlice(slice_item) if slice_item == self.tcx.types.u8 => {
129131
self.check_primitive_impl(def_id,
130132
lang_items.slice_u8_impl(),
133+
lang_items.slice_u8_alloc_impl(),
131134
"slice_u8",
132135
"[u8]",
133136
item.span);
134137
}
135138
ty::TySlice(_) => {
136139
self.check_primitive_impl(def_id,
137140
lang_items.slice_impl(),
141+
None,
138142
"slice",
139143
"[T]",
140144
item.span);
141145
}
142146
ty::TyRawPtr(ty::TypeAndMut { ty: _, mutbl: hir::MutImmutable }) => {
143147
self.check_primitive_impl(def_id,
144148
lang_items.const_ptr_impl(),
149+
None,
145150
"const_ptr",
146151
"*const T",
147152
item.span);
148153
}
149154
ty::TyRawPtr(ty::TypeAndMut { ty: _, mutbl: hir::MutMutable }) => {
150155
self.check_primitive_impl(def_id,
151156
lang_items.mut_ptr_impl(),
157+
None,
152158
"mut_ptr",
153159
"*mut T",
154160
item.span);
155161
}
156162
ty::TyInt(ast::IntTy::I8) => {
157163
self.check_primitive_impl(def_id,
158164
lang_items.i8_impl(),
165+
None,
159166
"i8",
160167
"i8",
161168
item.span);
162169
}
163170
ty::TyInt(ast::IntTy::I16) => {
164171
self.check_primitive_impl(def_id,
165172
lang_items.i16_impl(),
173+
None,
166174
"i16",
167175
"i16",
168176
item.span);
169177
}
170178
ty::TyInt(ast::IntTy::I32) => {
171179
self.check_primitive_impl(def_id,
172180
lang_items.i32_impl(),
181+
None,
173182
"i32",
174183
"i32",
175184
item.span);
176185
}
177186
ty::TyInt(ast::IntTy::I64) => {
178187
self.check_primitive_impl(def_id,
179188
lang_items.i64_impl(),
189+
None,
180190
"i64",
181191
"i64",
182192
item.span);
183193
}
184194
ty::TyInt(ast::IntTy::I128) => {
185195
self.check_primitive_impl(def_id,
186196
lang_items.i128_impl(),
197+
None,
187198
"i128",
188199
"i128",
189200
item.span);
190201
}
191202
ty::TyInt(ast::IntTy::Isize) => {
192203
self.check_primitive_impl(def_id,
193204
lang_items.isize_impl(),
205+
None,
194206
"isize",
195207
"isize",
196208
item.span);
197209
}
198210
ty::TyUint(ast::UintTy::U8) => {
199211
self.check_primitive_impl(def_id,
200212
lang_items.u8_impl(),
213+
None,
201214
"u8",
202215
"u8",
203216
item.span);
204217
}
205218
ty::TyUint(ast::UintTy::U16) => {
206219
self.check_primitive_impl(def_id,
207220
lang_items.u16_impl(),
221+
None,
208222
"u16",
209223
"u16",
210224
item.span);
211225
}
212226
ty::TyUint(ast::UintTy::U32) => {
213227
self.check_primitive_impl(def_id,
214228
lang_items.u32_impl(),
229+
None,
215230
"u32",
216231
"u32",
217232
item.span);
218233
}
219234
ty::TyUint(ast::UintTy::U64) => {
220235
self.check_primitive_impl(def_id,
221236
lang_items.u64_impl(),
237+
None,
222238
"u64",
223239
"u64",
224240
item.span);
225241
}
226242
ty::TyUint(ast::UintTy::U128) => {
227243
self.check_primitive_impl(def_id,
228244
lang_items.u128_impl(),
245+
None,
229246
"u128",
230247
"u128",
231248
item.span);
232249
}
233250
ty::TyUint(ast::UintTy::Usize) => {
234251
self.check_primitive_impl(def_id,
235252
lang_items.usize_impl(),
253+
None,
236254
"usize",
237255
"usize",
238256
item.span);
239257
}
240258
ty::TyFloat(ast::FloatTy::F32) => {
241259
self.check_primitive_impl(def_id,
242260
lang_items.f32_impl(),
261+
None,
243262
"f32",
244263
"f32",
245264
item.span);
246265
}
247266
ty::TyFloat(ast::FloatTy::F64) => {
248267
self.check_primitive_impl(def_id,
249268
lang_items.f64_impl(),
269+
None,
250270
"f64",
251271
"f64",
252272
item.span);
@@ -305,11 +325,15 @@ impl<'a, 'tcx> InherentCollect<'a, 'tcx> {
305325
fn check_primitive_impl(&self,
306326
impl_def_id: DefId,
307327
lang_def_id: Option<DefId>,
328+
lang_def_id2: Option<DefId>,
308329
lang: &str,
309330
ty: &str,
310331
span: Span) {
311-
match lang_def_id {
312-
Some(lang_def_id) if lang_def_id == impl_def_id => {
332+
match (lang_def_id, lang_def_id2) {
333+
(Some(lang_def_id), _) if lang_def_id == impl_def_id => {
334+
// OK
335+
}
336+
(_, Some(lang_def_id)) if lang_def_id == impl_def_id => {
313337
// OK
314338
}
315339
_ => {

src/librustdoc/clean/inline.rs

+1
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ pub fn build_impls(cx: &DocContext, did: DefId, auto_traits: bool) -> Vec<clean:
293293
lang_items.str_impl(),
294294
lang_items.slice_impl(),
295295
lang_items.slice_u8_impl(),
296+
lang_items.slice_u8_alloc_impl(),
296297
lang_items.const_ptr_impl(),
297298
lang_items.mut_ptr_impl(),
298299
];

0 commit comments

Comments
 (0)