1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
// Copyright 2018 Eduardo Sánchez Muñoz
//
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms. 

use core;
#[cfg(not(feature="no_std"))]
use std;

#[cfg(not(feature="no_std"))]
use ByteString;

use iterators;

use IntoMatcher;
use PrefixMatcher;
use SufixMatcher;
use ForwardSearcher;
use ReverseSearcher;

/// Borrowed reference to a byte string. It provides similar functionality as `str`
/// and `[u8]`.
#[derive(PartialEq, Eq)]
pub struct ByteStr {
    inner: [u8],
}

impl ByteStr {
    /// Creates an empty `ByteStr`.
    #[inline]
    pub fn empty<'a>() -> &'a Self {
        Self::from_slice(&[])
    }
    
    /// Creates an empty mutable `ByteStr`.
    #[inline]
    pub fn empty_mut<'a>() -> &'a mut Self {
        Self::from_slice_mut(&mut [])
    }
    
    /// Creates a `ByteStr` from a byte slice.
    #[inline]
    pub fn from_slice(bytes: &[u8]) -> &Self {
        unsafe { core::mem::transmute(bytes) }
    }
    
    /// Create a mutable `ByteStr` from a byte slice.
    #[inline]
    pub fn from_slice_mut(bytes: &mut [u8]) -> &mut Self {
        unsafe { core::mem::transmute(bytes) }
    }
    
    /// Forms a `ByteStr` from a pointer and a length.
    #[inline]
    pub unsafe fn from_raw_parts<'a>(ptr: *const u8, len: usize) -> &'a Self {
        ByteStr::from_slice(core::slice::from_raw_parts(ptr, len))
    }
    
    /// Forms a mutable `ByteStr` from a pointer and a length.
    #[inline]
    pub unsafe fn from_raw_parts_mut<'a>(ptr: *mut u8, len: usize) -> &'a mut Self {
        ByteStr::from_slice_mut(core::slice::from_raw_parts_mut(ptr, len))
    }
    
    /// Converts `self` into a byte slice.
    #[inline]
    pub fn as_slice(&self) -> &[u8] {
        &self.inner
    }
    
    /// Converts `self` into a mutable byte slice.
    #[inline]
    pub fn as_mut_slice(&mut self) -> &mut [u8] {
        &mut self.inner
    }
    
    /// Copies the `self` into a `Vec`.
    #[cfg(not(feature="no_std"))]
    #[inline]
    pub fn to_vec(&self) -> Vec<u8> {
        self.as_slice().to_vec()
    }
    
    /// Copies the `self` into a `ByteString`.
    #[cfg(not(feature="no_std"))]
    #[inline]
    pub fn to_byte_string(&self) -> ByteString {
        ByteString::from_vec(self.to_vec())
    }
    
    /// Converts `self` into a boxed slice without clones or allocation.
    #[cfg(not(feature="no_std"))]
    pub fn into_boxed_slice(self: Box<Self>) -> Box<[u8]> {
        unsafe { Box::from_raw(Box::into_raw(self) as *mut [u8]) }
    }
    
    /// Converts `self` into a vector without clones or allocation.
    #[cfg(not(feature="no_std"))]
    pub fn into_vec(self: Box<Self>) -> Vec<u8> {
        self.into_boxed_slice().into_vec()
    }
    
    /// Converts `self` into a `ByteString` without clones or allocation.
    #[cfg(not(feature="no_std"))]
    pub fn into_byte_string(self: Box<Self>) -> ByteString {
        ByteString::from_vec(self.into_vec())
    }
    
    /// Returns the length of `self`.
    #[inline]
    pub fn len(&self) -> usize {
        self.as_slice().len()
    }
    
    /// Returns `true` if the length of `self` is zero.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
    
    /// Converts `self` into a raw pointer that points to the first byte of the string.
    #[inline]
    pub fn as_ptr(&self) -> *const u8 {
        self.as_slice().as_ptr()
    }
    
    /// Converts `self` into a mutable raw pointer that points to the first byte of the string.
    #[inline]
    pub fn as_mut_ptr(&mut self) -> *mut u8 {
        self.as_mut_slice().as_mut_ptr()
    }
    
    /// Returns a reference to an element of the slice, or `None` if the
    /// index is out of bounds.
    #[inline]
    pub fn get(&self, index: usize) -> Option<&u8> {
        self.as_slice().get(index)
    }
    
    /// Returns a mutable reference to an element of the slice, or `None` if the
    /// index is out of bounds.
    #[inline]
    pub fn get_mut(&mut self, index: usize) -> Option<&mut u8> {
        self.as_mut_slice().get_mut(index)
    }
    
    /// Returns a reference to the first byte of the string, or `None` if it is empty.
    #[inline]
    pub fn first(&self) -> Option<&u8> {
        self.as_slice().first()
    }
    
    /// Returns a mutable reference to the first byte of the string, or `None` if it is empty.
    #[inline]
    pub fn first_mut(&mut self) -> Option<&mut u8> {
        self.as_mut_slice().first_mut()
    }
    
    /// Returns a reference to the last byte of the string, or `None` if it is empty.
    #[inline]
    pub fn last(&self) -> Option<&u8> {
        self.as_slice().last()
    }
    
    /// Returns a mutable reference to the last byte of the string, or `None` if it is empty.
    #[inline]
    pub fn last_mut(&mut self) -> Option<&mut u8> {
        self.as_mut_slice().last_mut()
    }
    
    /// Returns the first and all the rest of the bytes of the slice, or `None` if it is empty.
    #[inline]
    pub fn split_first(&self) -> Option<(&u8, &ByteStr)> {
        self.as_slice().split_first().map(|(f, r)| (f, Self::from_slice(r)))
    }
    
    /// Returns the first and all the rest of the bytes of the slice, or `None` if it is empty.
    #[inline]
    pub fn split_first_mut(&mut self) -> Option<(&mut u8, &mut ByteStr)> {
        self.as_mut_slice().split_first_mut().map(|(f, r)| (f, Self::from_slice_mut(r)))
    }
    
    /// Returns the last and all the rest of the bytes of the slice, or `None` if it is empty.
    #[inline]
    pub fn split_last(&self) -> Option<(&u8, &ByteStr)> {
        self.as_slice().split_last().map(|(f, r)| (f, Self::from_slice(r)))
    }
    
    /// Returns the last and all the rest of the bytes of the slice, or `None` if it is empty.
    #[inline]
    pub fn split_last_mut(&mut self) -> Option<(&mut u8, &mut ByteStr)> {
        self.as_mut_slice().split_last_mut().map(|(f, r)| (f, Self::from_slice_mut(r)))
    }
    
    /// Returns an iterator over the string.
    #[inline]
    pub fn iter<'a>(&'a self) -> core::slice::Iter<'a, u8> {
        self.as_slice().iter()
    }
    
    /// Returns an iterator that allows modifying each value.
    #[inline]
    pub fn iter_mut<'a>(&'a mut self) -> core::slice::IterMut<'a, u8> {
        self.as_mut_slice().iter_mut()
    }
    
    /// Returns an iterator over all contiguous windows of length `size`.
    /// The windows overlap. If the string is shorter than `size`, the
    /// iterator returns no values.
    ///
    /// Similar to `slice::windows()`.
    #[inline]
    pub fn windows<'a>(&'a self, size: usize) -> iterators::Windows<'a> {
        iterators::Windows::new(self, size)
    }
    
    /// Returns an iterator over `size` bytes of the string at a time. The chunks do not
    /// overlap. If size does not divide the length of the slice, then the last chunk will
    /// not have length `size`.
    ///
    /// Similar to `slice::chunks()`.
    #[inline]
    pub fn chunks<'a>(&'a self, size: usize) -> iterators::Chunks<'a> {
        iterators::Chunks::new(self, size)
    }
    
    /// Returns an iterator over `size` elements of the slice at a time. The chunks are mutable
    /// strings and do not overlap. If `size` does not divide the length of the slice, then the
    /// last chunk will not have length `size`.
    ///
    /// Similar to `slice::chunks_mut()`.
    #[inline]
    pub fn chunks_mut<'a>(&'a mut self, size: usize) -> iterators::ChunksMut<'a> {
        iterators::ChunksMut::new(self, size)
    }
    
    /// Divides one string into two at an index.
    ///
    /// The first will contain all indices from `[0, mid)` (excluding the index `mid` itself) and
    /// the second will contain all indices from `[mid, len)` (excluding the index `len` itself).
    ///
    /// Similar to `slice::split_at()`.
    ///
    /// # Panics
    ///
    /// Panics if `mid > len`.
    #[inline]
    pub fn split_at(&self, mid: usize) -> (&ByteStr, &ByteStr) {
        let (first, second) = self.as_slice().split_at(mid);
        (ByteStr::from_slice(first), ByteStr::from_slice(second))
    }
    
    /// Divides one `&mut` string into two at an index.
    ///
    /// The first will contain all indices from `[0, mid)` (excluding the index `mid` itself) and
    /// the second will contain all indices from `[mid, len)` (excluding the index `len` itself).
    ///
    /// Similar to `slice::split_at_mut()`.
    ///
    /// # Panics
    ///
    /// Panics if `mid > len`.
    #[inline]
    pub fn split_at_mut(&mut self, mid: usize) -> (&mut ByteStr, &mut ByteStr) {
        let (first, second) = self.as_mut_slice().split_at_mut(mid);
        (ByteStr::from_slice_mut(first), ByteStr::from_slice_mut(second))
    }
    
    /// Returns an iterator over substrings of this string, separated by a matcher.
    #[inline]
    pub fn split<'a, M: IntoMatcher>(&'a self, m: M) -> iterators::Split<'a, M::Matcher>
        where <M as IntoMatcher>::Matcher: ForwardSearcher
    {
        iterators::Split::new(self, m.into_matcher())
    }
    
    /// Returns an iterator over mutable substrings of this string, separated by a matcher.
    #[inline]
    pub fn split_mut<'a, M: IntoMatcher>(&'a mut self, m: M) -> iterators::SplitMut<'a, M::Matcher>
        where <M as IntoMatcher>::Matcher: ForwardSearcher
    {
        iterators::SplitMut::new(self, m.into_matcher())
    }
    
    /// Returns an iterator over substrings of this string, separated by a matcher,
    /// starting at the end of the slice and working backwards.
    #[inline]
    pub fn rsplit<'a, M: IntoMatcher>(&'a self, m: M) -> iterators::RSplit<'a, M::Matcher>
        where <M as IntoMatcher>::Matcher: ReverseSearcher
    {
        iterators::RSplit::new(self, m.into_matcher())
    }
    
    /// Returns an iterator over mutable substrings of this string, separated by a matcher,
    /// starting at the end of the slice and working backwards.
    #[inline]
    pub fn rsplit_mut<'a, M: IntoMatcher>(&'a mut self, m: M) -> iterators::RSplitMut<'a, M::Matcher>
        where <M as IntoMatcher>::Matcher: ReverseSearcher
    {
        iterators::RSplitMut::new(self, m.into_matcher())
    }
    
    /// Returns an iterator over substrings of this string, separated by a matcher, returning
    /// at most `n` items.
    ///
    /// If `n` substrings are returned, the last substring will contain the remainder of the string.
    #[inline]
    pub fn splitn<'a, M: IntoMatcher>(&'a self, n: usize, m: M) -> iterators::SplitN<'a, M::Matcher>
        where <M as IntoMatcher>::Matcher: ForwardSearcher
    {
        iterators::SplitN::new(self, n, m.into_matcher())
    }
    
    /// Returns an iterator over mutable substrings of this string, separated by a matcher, returning
    /// at most `n` items.
    ///
    /// If `n` substrings are returned, the last substring will contain the remainder of the string.
    #[inline]
    pub fn splitn_mut<'a, M: IntoMatcher>(&'a mut self, n: usize, m: M) -> iterators::SplitNMut<'a, M::Matcher>
        where <M as IntoMatcher>::Matcher: ForwardSearcher
    {
        iterators::SplitNMut::new(self, n, m.into_matcher())
    }
    
    /// Returns an iterator over substrings of this string, separated by a matcher and stating from
    /// the end of the string, returning at most `n` items.
    ///
    /// If `n` substrings are returned, the last substring will contain the remainder of the string.
    #[inline]
    pub fn rsplitn<'a, M: IntoMatcher>(&'a self, n: usize, m: M) -> iterators::RSplitN<'a, M::Matcher>
        where <M as IntoMatcher>::Matcher: ReverseSearcher
    {
        iterators::RSplitN::new(self, n, m.into_matcher())
    }
    
    /// Returns an iterator over mutable substrings of this string, separated by a matcher and stating from
    /// the end of the string, returning at most `n` items.
    ///
    /// If `n` substrings are returned, the last substring will contain the remainder of the string.
    #[inline]
    pub fn rsplitn_mut<'a, M: IntoMatcher>(&'a mut self, n: usize, m: M) -> iterators::RSplitNMut<'a, M::Matcher>
        where <M as IntoMatcher>::Matcher: ReverseSearcher
    {
        iterators::RSplitNMut::new(self, n, m.into_matcher())
    }
    
    /// Returns an iterator over the disjoint matches within the given string.
    #[inline]
    pub fn matches<'a, M: IntoMatcher>(&'a self, m: M) -> iterators::Matches<'a, M::Matcher>
        where <M as IntoMatcher>::Matcher: ForwardSearcher
    {
        iterators::Matches::new(self, m.into_matcher())
    }
    
    /// Returns an iterator over the mutable disjoint matches within the given string.
    #[inline]
    pub fn matches_mut<'a, M: IntoMatcher>(&'a mut self, m: M) -> iterators::MatchesMut<'a, M::Matcher>
        where <M as IntoMatcher>::Matcher: ForwardSearcher
    {
        iterators::MatchesMut::new(self, m.into_matcher())
    }
    
    /// Returns an iterator over the disjoint matches within the given string, yielded in reverse order.
    #[inline]
    pub fn rmatches<'a, M: IntoMatcher>(&'a self, m: M) -> iterators::RMatches<'a, M::Matcher>
        where <M as IntoMatcher>::Matcher: ReverseSearcher
    {
        iterators::RMatches::new(self, m.into_matcher())
    }
    
    /// Returns an iterator over the mutable disjoint matches within the given string, yielded
    /// in reverse order.
    #[inline]
    pub fn rmatches_mut<'a, M: IntoMatcher>(&'a mut self, m: M) -> iterators::RMatchesMut<'a, M::Matcher>
        where <M as IntoMatcher>::Matcher: ReverseSearcher
    {
        iterators::RMatchesMut::new(self, m.into_matcher())
    }
    
    /// Returns an iterator over the disjoint matches within the given string, as well as the index
    /// that the match starts at.
    #[inline]
    pub fn match_indices<'a, M: IntoMatcher>(&'a self, m: M) -> iterators::MatchIndices<'a, M::Matcher>
        where <M as IntoMatcher>::Matcher: ForwardSearcher
    {
        iterators::MatchIndices::new(self, m.into_matcher())
    }
    
    /// Returns an iterator over the mutable disjoint matches within the given string, as well as
    /// the index that the match starts at.
    #[inline]
    pub fn match_indices_mut<'a, M: IntoMatcher>(&'a mut self, m: M) -> iterators::MatchIndicesMut<'a, M::Matcher>
        where <M as IntoMatcher>::Matcher: ForwardSearcher
    {
        iterators::MatchIndicesMut::new(self, m.into_matcher())
    }
    
    /// Returns an iterator over the disjoint matches within the given string, yielded in reverse order,
    /// as well as the index that the match starts at.
    #[inline]
    pub fn rmatch_indices<'a, M: IntoMatcher>(&'a self, m: M) -> iterators::RMatchIndices<'a, M::Matcher>
        where <M as IntoMatcher>::Matcher: ReverseSearcher
    {
        iterators::RMatchIndices::new(self, m.into_matcher())
    }
    
    /// Returns an iterator over the mutable disjoint matches within the given string, yielded in reverse
    /// order, as well as the index that the match starts at.
    #[inline]
    pub fn rmatch_indices_mut<'a, M: IntoMatcher>(&'a mut self, m: M) -> iterators::RMatchIndicesMut<'a, M::Matcher>
        where <M as IntoMatcher>::Matcher: ReverseSearcher
    {
        iterators::RMatchIndicesMut::new(self, m.into_matcher())
    }
    
    /// Returns `true` if the string contains a substring that matches the given matcher.
    #[inline]
    pub fn contains<M: IntoMatcher>(&self, m: M) -> bool
        where <M as IntoMatcher>::Matcher: ForwardSearcher
    {
        m.into_matcher().find(self).is_some()
    }
    
    /// Returns `true` if the string beginning a matches the given matcher.
    #[inline]
    pub fn starts_with<M: IntoMatcher>(&self, m: M) -> bool
        where <M as IntoMatcher>::Matcher: PrefixMatcher
    {
        m.into_matcher().is_prefix_of(self)
    }
    
    /// Returns `true` if the string ending a matches the given matcher.
    #[inline]
    pub fn ends_with<M: IntoMatcher>(&self, m: M) -> bool
        where <M as IntoMatcher>::Matcher: SufixMatcher
    {
        m.into_matcher().is_sufix_of(self)
    }
    
    /// Returns the byte index of the first character of `self` that matches the
    /// matcher or `None` it it doesn't match.
    #[inline]
    pub fn find<M: IntoMatcher>(&self, m: M) -> Option<usize>
        where <M as IntoMatcher>::Matcher: ForwardSearcher
    {
        m.into_matcher().find(self).map(|(a, _)| a)
    }
    
    /// Returns the byte index of the last character of `self` that matches the
    /// matcher or `None` it it doesn't match.
    #[inline]
    pub fn rfind<M: IntoMatcher>(&self, m: M) -> Option<usize>
        where <M as IntoMatcher>::Matcher: ReverseSearcher
    {
        m.into_matcher().rfind(self).map(|(_, b)| b)
    }
    
    /// Swaps two bytes in the string, indexed by `a` and `b`.
    ///
    /// # Panics
    ///
    /// Panics if `a` or `b` are out of bounds.
    #[inline]
    pub fn swap(&mut self, a: usize, b: usize) {
        self.as_mut_slice().swap(a, b);
    }
    
    /// Reverses the order of bytes in the slice.
    #[inline]
    pub fn reverse(&mut self) {
        self.as_mut_slice().reverse();
    }
    
    /// Copies all elements from `src` into `self`, using a memcpy.
    ///
    /// The length of `src` must be the same as `self`.
    #[inline]
    pub fn copy_from_slice(&mut self, src: &[u8]) {
        self.as_mut_slice().copy_from_slice(src);
    }
    
    /// Copies all elements from `src` into `self`, using a memcpy.
    ///
    /// The length of `src` must be the same as `self`.
    #[inline]
    pub fn copy_from_byte_str(&mut self, src: &ByteStr) {
        self.as_mut_slice().copy_from_slice(src.as_slice());
    }
}

// Default
impl<'a> Default for &'a ByteStr {
    #[inline]
    fn default() -> Self {
        ByteStr::empty()
    }
}

impl<'a> Default for &'a mut ByteStr {
    #[inline]
    fn default() -> Self {
        ByteStr::empty_mut()
    }
}

// Debug
impl core::fmt::Debug for ByteStr {
    fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
        use core::fmt::Write;
        
        fn to_hex(nibble: u8) -> u8 {
            if nibble < 10 {
                b'0' + nibble
            } else {
                b'a' + nibble - 10
            }
        }
        
        f.write_str("b\"")?;
        for &byte in self.iter() {
            match byte {
                b'\t' => f.write_str("\\t")?,
                b'\r' => f.write_str("\\r")?,
                b'\n' => f.write_str("\\n")?,
                b'\\' => f.write_str("\\\\")?,
                b'"' => f.write_str("\\\"")?,
                0x20 ... 0x7E => f.write_char(byte as char)?,
                _ => {
                    f.write_str("\\x")?;
                    f.write_char(to_hex(byte >> 4) as char)?;
                    f.write_char(to_hex(byte & 0xF) as char)?;
                }
            }
        }
        f.write_char('"')?;
        Ok(())
    }
}

// ToOwned
#[cfg(not(feature="no_std"))]
impl std::borrow::ToOwned for ByteStr {
    type Owned = ByteString;
    
    #[inline]
    fn to_owned(&self) -> ByteString {
        self.to_byte_string()
    }
}

// AsRef
impl core::convert::AsRef<ByteStr> for ByteStr {
    #[inline]
    fn as_ref(&self) -> &ByteStr {
        self
    }
}

impl core::convert::AsRef<[u8]> for ByteStr {
    #[inline]
    fn as_ref(&self) -> &[u8] {
        self.as_slice()
    }
}

// AsMut
impl core::convert::AsMut<ByteStr> for ByteStr {
    #[inline]
    fn as_mut(&mut self) -> &mut ByteStr {
        self
    }
}

impl core::convert::AsMut<[u8]> for ByteStr {
    #[inline]
    fn as_mut(&mut self) -> &mut [u8] {
        self.as_mut_slice()
    }
}

// PartialEq
impl<'a> core::cmp::PartialEq<ByteStr> for &'a ByteStr {
    #[inline]
    fn eq(&self, other: &ByteStr) -> bool {
        self.as_slice() == other.as_slice()
    }
}

impl core::cmp::PartialEq<[u8]> for ByteStr {
    #[inline]
    fn eq(&self, other: &[u8]) -> bool {
        self.as_slice() == other
    }
}

impl<'a> core::cmp::PartialEq<[u8]> for &'a ByteStr {
    #[inline]
    fn eq(&self, other: &[u8]) -> bool {
        self.as_slice() == other
    }
}

macro_rules! impl_partial_eq_array {
    ($size:expr) => {
        impl core::cmp::PartialEq<[u8; $size]> for ByteStr {
            #[inline]
            fn eq(&self, other: &[u8; $size]) -> bool {
                self.as_slice() == other
            }
        }
        
        impl<'a> core::cmp::PartialEq<[u8; $size]> for &'a ByteStr {
            #[inline]
            fn eq(&self, other: &[u8; $size]) -> bool {
                self.as_slice() == other
            }
        }
    }
}

impl_partial_eq_array!(0);
impl_partial_eq_array!(1);
impl_partial_eq_array!(2);
impl_partial_eq_array!(3);
impl_partial_eq_array!(4);
impl_partial_eq_array!(5);
impl_partial_eq_array!(6);
impl_partial_eq_array!(7);
impl_partial_eq_array!(8);
impl_partial_eq_array!(9);
impl_partial_eq_array!(10);
impl_partial_eq_array!(11);
impl_partial_eq_array!(12);
impl_partial_eq_array!(13);
impl_partial_eq_array!(14);
impl_partial_eq_array!(15);
impl_partial_eq_array!(16);
impl_partial_eq_array!(17);
impl_partial_eq_array!(18);
impl_partial_eq_array!(19);
impl_partial_eq_array!(20);
impl_partial_eq_array!(21);
impl_partial_eq_array!(22);
impl_partial_eq_array!(23);
impl_partial_eq_array!(24);
impl_partial_eq_array!(25);
impl_partial_eq_array!(26);
impl_partial_eq_array!(27);
impl_partial_eq_array!(28);
impl_partial_eq_array!(29);
impl_partial_eq_array!(30);
impl_partial_eq_array!(31);
impl_partial_eq_array!(32);

#[cfg(not(feature="no_std"))]
impl core::cmp::PartialEq<ByteString> for ByteStr {
    #[inline]
    fn eq(&self, other: &ByteString) -> bool {
        self.as_slice() == other.as_slice()
    }
}

#[cfg(not(feature="no_std"))]
impl<'a> core::cmp::PartialEq<ByteString> for &'a ByteStr {
    #[inline]
    fn eq(&self, other: &ByteString) -> bool {
        self.as_slice() == other.as_slice()
    }
}

#[cfg(not(feature="no_std"))]
impl<'b> core::cmp::PartialEq<std::borrow::Cow<'b, ByteStr>> for ByteStr {
    #[inline]
    fn eq(&self, other: &std::borrow::Cow<'b, ByteStr>) -> bool {
        self.as_slice() == other.as_slice()
    }
}

#[cfg(not(feature="no_std"))]
impl<'a, 'b> core::cmp::PartialEq<std::borrow::Cow<'b, ByteStr>> for &'a ByteStr {
    #[inline]
    fn eq(&self, other: &std::borrow::Cow<'b, ByteStr>) -> bool {
        self.as_slice() == other.as_slice()
    }
}

impl<'a, T: ?Sized> core::cmp::PartialEq<&'a T> for ByteStr
    where ByteStr: core::cmp::PartialEq<T>
{
    #[inline]
    fn eq(&self, other: &&'a T) -> bool {
        self == *other
    }
}

// Index
impl core::ops::Index<usize> for ByteStr {
    type Output = u8;
    
    #[inline]
    fn index(&self, index: usize) -> &u8 {
        &self.as_slice()[index]
    }
}

impl core::ops::Index<core::ops::Range<usize>> for ByteStr {
    type Output = ByteStr;
    
    #[inline]
    fn index(&self, index: core::ops::Range<usize>) -> &ByteStr {
        ByteStr::from_slice(&self.as_slice()[index])
    }
}

impl core::ops::Index<core::ops::RangeFrom<usize>> for ByteStr {
    type Output = ByteStr;
    
    #[inline]
    fn index(&self, index: core::ops::RangeFrom<usize>) -> &ByteStr {
        ByteStr::from_slice(&self.as_slice()[index])
    }
}

impl core::ops::Index<core::ops::RangeTo<usize>> for ByteStr {
    type Output = ByteStr;
    
    #[inline]
    fn index(&self, index: core::ops::RangeTo<usize>) -> &ByteStr {
        ByteStr::from_slice(&self.as_slice()[index])
    }
}

impl core::ops::Index<core::ops::RangeFull> for ByteStr {
    type Output = ByteStr;
    
    #[inline]
    fn index(&self, index: core::ops::RangeFull) -> &ByteStr {
        ByteStr::from_slice(&self.as_slice()[index])
    }
}

// IndexMux
impl core::ops::IndexMut<usize> for ByteStr {
    #[inline]
    fn index_mut(&mut self, index: usize) -> &mut u8 {
        &mut self.as_mut_slice()[index]
    }
}

impl core::ops::IndexMut<core::ops::Range<usize>> for ByteStr {
    #[inline]
    fn index_mut(&mut self, index: core::ops::Range<usize>) -> &mut ByteStr {
        ByteStr::from_slice_mut(&mut self.as_mut_slice()[index])
    }
}

impl core::ops::IndexMut<core::ops::RangeFrom<usize>> for ByteStr {
    #[inline]
    fn index_mut(&mut self, index: core::ops::RangeFrom<usize>) -> &mut ByteStr {
        ByteStr::from_slice_mut(&mut self.as_mut_slice()[index])
    }
}

impl core::ops::IndexMut<core::ops::RangeTo<usize>> for ByteStr {
    #[inline]
    fn index_mut(&mut self, index: core::ops::RangeTo<usize>) -> &mut ByteStr {
        ByteStr::from_slice_mut(&mut self.as_mut_slice()[index])
    }
}

impl core::ops::IndexMut<core::ops::RangeFull> for ByteStr {
    #[inline]
    fn index_mut(&mut self, index: core::ops::RangeFull) -> &mut ByteStr {
        ByteStr::from_slice_mut(&mut self.as_mut_slice()[index])
    }
}

// IntoIterator
impl<'a> core::iter::IntoIterator for &'a ByteStr {
    type Item = &'a u8;
    type IntoIter = core::slice::Iter<'a, u8>;
    
    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

impl<'a> core::iter::IntoIterator for &'a mut ByteStr {
    type Item = &'a mut u8;
    type IntoIter = core::slice::IterMut<'a, u8>;
    
    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.iter_mut()
    }
}