Skip to content

Commit 56ffb43

Browse files
authored
Rollup merge of #138381 - thaliaarchi:bstr-sliceindex, r=joshtriplett
Implement `SliceIndex` for `ByteStr` Implement `Index` and `IndexMut` for `ByteStr` in terms of `SliceIndex`. Implement it for the same types that `&[u8]` supports (a superset of those supported for `&str`, which does not have `usize` and `ops::IndexRange`). At the same time, move compare and index traits to a separate file in the `bstr` module, to give it more space to grow as more functionality is added (e.g., iterators and string-like ops). Order the items in `bstr/traits.rs` similarly to `str/traits.rs`. cc `@joshtriplett` `ByteStr`/`ByteString` tracking issue: #134915
2 parents 543160d + 9d379e1 commit 56ffb43

File tree

9 files changed

+352
-315
lines changed

9 files changed

+352
-315
lines changed

Diff for: library/core/src/bstr.rs renamed to library/core/src/bstr/mod.rs

+14-273
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
//! The `ByteStr` type and trait implementations.
22
3+
mod traits;
4+
5+
#[unstable(feature = "bstr_internals", issue = "none")]
6+
pub use traits::{impl_partial_eq, impl_partial_eq_n, impl_partial_eq_ord};
7+
38
use crate::borrow::{Borrow, BorrowMut};
4-
use crate::cmp::Ordering;
5-
use crate::ops::{
6-
Deref, DerefMut, DerefPure, Index, IndexMut, Range, RangeFrom, RangeFull, RangeInclusive,
7-
RangeTo, RangeToInclusive,
8-
};
9-
use crate::{fmt, hash};
9+
use crate::fmt;
10+
use crate::ops::{Deref, DerefMut, DerefPure};
1011

1112
/// A wrapper for `&[u8]` representing a human-readable string that's conventionally, but not
1213
/// always, UTF-8.
@@ -91,6 +92,13 @@ impl ByteStr {
9192
pub fn as_bytes(&self) -> &[u8] {
9293
&self.0
9394
}
95+
96+
#[doc(hidden)]
97+
#[unstable(feature = "bstr_internals", issue = "none")]
98+
#[inline]
99+
pub fn as_bytes_mut(&mut self) -> &mut [u8] {
100+
&mut self.0
101+
}
94102
}
95103

96104
#[unstable(feature = "bstr", issue = "134915")]
@@ -295,273 +303,6 @@ impl<'a> Default for &'a mut ByteStr {
295303
// }
296304
// }
297305

298-
#[unstable(feature = "bstr", issue = "134915")]
299-
impl hash::Hash for ByteStr {
300-
#[inline]
301-
fn hash<H: hash::Hasher>(&self, state: &mut H) {
302-
self.0.hash(state);
303-
}
304-
}
305-
306-
#[unstable(feature = "bstr", issue = "134915")]
307-
impl Index<usize> for ByteStr {
308-
type Output = u8;
309-
310-
#[inline]
311-
fn index(&self, idx: usize) -> &u8 {
312-
&self.0[idx]
313-
}
314-
}
315-
316-
#[unstable(feature = "bstr", issue = "134915")]
317-
impl Index<RangeFull> for ByteStr {
318-
type Output = ByteStr;
319-
320-
#[inline]
321-
fn index(&self, _: RangeFull) -> &ByteStr {
322-
self
323-
}
324-
}
325-
326-
#[unstable(feature = "bstr", issue = "134915")]
327-
impl Index<Range<usize>> for ByteStr {
328-
type Output = ByteStr;
329-
330-
#[inline]
331-
fn index(&self, r: Range<usize>) -> &ByteStr {
332-
ByteStr::from_bytes(&self.0[r])
333-
}
334-
}
335-
336-
#[unstable(feature = "bstr", issue = "134915")]
337-
impl Index<RangeInclusive<usize>> for ByteStr {
338-
type Output = ByteStr;
339-
340-
#[inline]
341-
fn index(&self, r: RangeInclusive<usize>) -> &ByteStr {
342-
ByteStr::from_bytes(&self.0[r])
343-
}
344-
}
345-
346-
#[unstable(feature = "bstr", issue = "134915")]
347-
impl Index<RangeFrom<usize>> for ByteStr {
348-
type Output = ByteStr;
349-
350-
#[inline]
351-
fn index(&self, r: RangeFrom<usize>) -> &ByteStr {
352-
ByteStr::from_bytes(&self.0[r])
353-
}
354-
}
355-
356-
#[unstable(feature = "bstr", issue = "134915")]
357-
impl Index<RangeTo<usize>> for ByteStr {
358-
type Output = ByteStr;
359-
360-
#[inline]
361-
fn index(&self, r: RangeTo<usize>) -> &ByteStr {
362-
ByteStr::from_bytes(&self.0[r])
363-
}
364-
}
365-
366-
#[unstable(feature = "bstr", issue = "134915")]
367-
impl Index<RangeToInclusive<usize>> for ByteStr {
368-
type Output = ByteStr;
369-
370-
#[inline]
371-
fn index(&self, r: RangeToInclusive<usize>) -> &ByteStr {
372-
ByteStr::from_bytes(&self.0[r])
373-
}
374-
}
375-
376-
#[unstable(feature = "bstr", issue = "134915")]
377-
impl IndexMut<usize> for ByteStr {
378-
#[inline]
379-
fn index_mut(&mut self, idx: usize) -> &mut u8 {
380-
&mut self.0[idx]
381-
}
382-
}
383-
384-
#[unstable(feature = "bstr", issue = "134915")]
385-
impl IndexMut<RangeFull> for ByteStr {
386-
#[inline]
387-
fn index_mut(&mut self, _: RangeFull) -> &mut ByteStr {
388-
self
389-
}
390-
}
391-
392-
#[unstable(feature = "bstr", issue = "134915")]
393-
impl IndexMut<Range<usize>> for ByteStr {
394-
#[inline]
395-
fn index_mut(&mut self, r: Range<usize>) -> &mut ByteStr {
396-
ByteStr::from_bytes_mut(&mut self.0[r])
397-
}
398-
}
399-
400-
#[unstable(feature = "bstr", issue = "134915")]
401-
impl IndexMut<RangeInclusive<usize>> for ByteStr {
402-
#[inline]
403-
fn index_mut(&mut self, r: RangeInclusive<usize>) -> &mut ByteStr {
404-
ByteStr::from_bytes_mut(&mut self.0[r])
405-
}
406-
}
407-
408-
#[unstable(feature = "bstr", issue = "134915")]
409-
impl IndexMut<RangeFrom<usize>> for ByteStr {
410-
#[inline]
411-
fn index_mut(&mut self, r: RangeFrom<usize>) -> &mut ByteStr {
412-
ByteStr::from_bytes_mut(&mut self.0[r])
413-
}
414-
}
415-
416-
#[unstable(feature = "bstr", issue = "134915")]
417-
impl IndexMut<RangeTo<usize>> for ByteStr {
418-
#[inline]
419-
fn index_mut(&mut self, r: RangeTo<usize>) -> &mut ByteStr {
420-
ByteStr::from_bytes_mut(&mut self.0[r])
421-
}
422-
}
423-
424-
#[unstable(feature = "bstr", issue = "134915")]
425-
impl IndexMut<RangeToInclusive<usize>> for ByteStr {
426-
#[inline]
427-
fn index_mut(&mut self, r: RangeToInclusive<usize>) -> &mut ByteStr {
428-
ByteStr::from_bytes_mut(&mut self.0[r])
429-
}
430-
}
431-
432-
#[unstable(feature = "bstr", issue = "134915")]
433-
impl Eq for ByteStr {}
434-
435-
#[unstable(feature = "bstr", issue = "134915")]
436-
impl PartialEq<ByteStr> for ByteStr {
437-
#[inline]
438-
fn eq(&self, other: &ByteStr) -> bool {
439-
&self.0 == &other.0
440-
}
441-
}
442-
443-
#[doc(hidden)]
444-
#[macro_export]
445-
#[unstable(feature = "bstr_internals", issue = "none")]
446-
macro_rules! impl_partial_eq {
447-
($lhs:ty, $rhs:ty) => {
448-
#[allow(unused_lifetimes)]
449-
impl<'a> PartialEq<$rhs> for $lhs {
450-
#[inline]
451-
fn eq(&self, other: &$rhs) -> bool {
452-
let other: &[u8] = other.as_ref();
453-
PartialEq::eq(self.as_bytes(), other)
454-
}
455-
}
456-
457-
#[allow(unused_lifetimes)]
458-
impl<'a> PartialEq<$lhs> for $rhs {
459-
#[inline]
460-
fn eq(&self, other: &$lhs) -> bool {
461-
let this: &[u8] = self.as_ref();
462-
PartialEq::eq(this, other.as_bytes())
463-
}
464-
}
465-
};
466-
}
467-
468-
#[doc(hidden)]
469-
#[unstable(feature = "bstr_internals", issue = "none")]
470-
pub use impl_partial_eq;
471-
472-
#[doc(hidden)]
473-
#[macro_export]
474-
#[unstable(feature = "bstr_internals", issue = "none")]
475-
macro_rules! impl_partial_eq_ord {
476-
($lhs:ty, $rhs:ty) => {
477-
$crate::bstr::impl_partial_eq!($lhs, $rhs);
478-
479-
#[allow(unused_lifetimes)]
480-
#[unstable(feature = "bstr", issue = "134915")]
481-
impl<'a> PartialOrd<$rhs> for $lhs {
482-
#[inline]
483-
fn partial_cmp(&self, other: &$rhs) -> Option<Ordering> {
484-
let other: &[u8] = other.as_ref();
485-
PartialOrd::partial_cmp(self.as_bytes(), other)
486-
}
487-
}
488-
489-
#[allow(unused_lifetimes)]
490-
#[unstable(feature = "bstr", issue = "134915")]
491-
impl<'a> PartialOrd<$lhs> for $rhs {
492-
#[inline]
493-
fn partial_cmp(&self, other: &$lhs) -> Option<Ordering> {
494-
let this: &[u8] = self.as_ref();
495-
PartialOrd::partial_cmp(this, other.as_bytes())
496-
}
497-
}
498-
};
499-
}
500-
501-
#[doc(hidden)]
502-
#[unstable(feature = "bstr_internals", issue = "none")]
503-
pub use impl_partial_eq_ord;
504-
505-
#[doc(hidden)]
506-
#[macro_export]
507-
#[unstable(feature = "bstr_internals", issue = "none")]
508-
macro_rules! impl_partial_eq_n {
509-
($lhs:ty, $rhs:ty) => {
510-
#[allow(unused_lifetimes)]
511-
#[unstable(feature = "bstr", issue = "134915")]
512-
impl<const N: usize> PartialEq<$rhs> for $lhs {
513-
#[inline]
514-
fn eq(&self, other: &$rhs) -> bool {
515-
let other: &[u8] = other.as_ref();
516-
PartialEq::eq(self.as_bytes(), other)
517-
}
518-
}
519-
520-
#[allow(unused_lifetimes)]
521-
#[unstable(feature = "bstr", issue = "134915")]
522-
impl<const N: usize> PartialEq<$lhs> for $rhs {
523-
#[inline]
524-
fn eq(&self, other: &$lhs) -> bool {
525-
let this: &[u8] = self.as_ref();
526-
PartialEq::eq(this, other.as_bytes())
527-
}
528-
}
529-
};
530-
}
531-
532-
#[doc(hidden)]
533-
#[unstable(feature = "bstr_internals", issue = "none")]
534-
pub use impl_partial_eq_n;
535-
536-
// PartialOrd with `[u8]` omitted to avoid inference failures
537-
impl_partial_eq!(ByteStr, [u8]);
538-
// PartialOrd with `&[u8]` omitted to avoid inference failures
539-
impl_partial_eq!(ByteStr, &[u8]);
540-
// PartialOrd with `str` omitted to avoid inference failures
541-
impl_partial_eq!(ByteStr, str);
542-
// PartialOrd with `&str` omitted to avoid inference failures
543-
impl_partial_eq!(ByteStr, &str);
544-
// PartialOrd with `[u8; N]` omitted to avoid inference failures
545-
impl_partial_eq_n!(ByteStr, [u8; N]);
546-
// PartialOrd with `[u8; N]` omitted to avoid inference failures
547-
impl_partial_eq_n!(ByteStr, &[u8; N]);
548-
549-
#[unstable(feature = "bstr", issue = "134915")]
550-
impl Ord for ByteStr {
551-
#[inline]
552-
fn cmp(&self, other: &ByteStr) -> Ordering {
553-
Ord::cmp(&self.0, &other.0)
554-
}
555-
}
556-
557-
#[unstable(feature = "bstr", issue = "134915")]
558-
impl PartialOrd for ByteStr {
559-
#[inline]
560-
fn partial_cmp(&self, other: &ByteStr) -> Option<Ordering> {
561-
PartialOrd::partial_cmp(&self.0, &other.0)
562-
}
563-
}
564-
565306
#[unstable(feature = "bstr", issue = "134915")]
566307
impl<'a> TryFrom<&'a ByteStr> for &'a str {
567308
type Error = crate::str::Utf8Error;

0 commit comments

Comments
 (0)