-
Notifications
You must be signed in to change notification settings - Fork 13.3k
/
Copy pathbstr.rs
698 lines (611 loc) · 19.5 KB
/
bstr.rs
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
//! The `ByteStr` and `ByteString` types and trait implementations.
// This could be more fine-grained.
#![cfg(not(no_global_oom_handling))]
use core::borrow::{Borrow, BorrowMut};
#[unstable(feature = "bstr", issue = "134915")]
pub use core::bstr::ByteStr;
use core::bstr::{impl_partial_eq, impl_partial_eq_n, impl_partial_eq_ord};
use core::cmp::Ordering;
use core::ops::{
Deref, DerefMut, DerefPure, Index, IndexMut, Range, RangeFrom, RangeFull, RangeInclusive,
RangeTo, RangeToInclusive,
};
use core::str::FromStr;
use core::{fmt, hash};
use crate::borrow::{Cow, ToOwned};
use crate::boxed::Box;
#[cfg(not(no_rc))]
use crate::rc::Rc;
use crate::string::String;
#[cfg(all(not(no_rc), not(no_sync), target_has_atomic = "ptr"))]
use crate::sync::Arc;
use crate::vec::Vec;
/// A wrapper for `Vec<u8>` representing a human-readable string that's conventionally, but not
/// always, UTF-8.
///
/// Unlike `String`, this type permits non-UTF-8 contents, making it suitable for user input,
/// non-native filenames (as `Path` only supports native filenames), and other applications that
/// need to round-trip whatever data the user provides.
///
/// A `ByteString` owns its contents and can grow and shrink, like a `Vec` or `String`. For a
/// borrowed byte string, see [`ByteStr`](../../std/bstr/struct.ByteStr.html).
///
/// `ByteString` implements `Deref` to `&Vec<u8>`, so all methods available on `&Vec<u8>` are
/// available on `ByteString`. Similarly, `ByteString` implements `DerefMut` to `&mut Vec<u8>`,
/// so you can modify a `ByteString` using any method available on `&mut Vec<u8>`.
///
/// The `Debug` and `Display` implementations for `ByteString` are the same as those for `ByteStr`,
/// showing invalid UTF-8 as hex escapes or the Unicode replacement character, respectively.
#[unstable(feature = "bstr", issue = "134915")]
#[repr(transparent)]
#[derive(Clone)]
#[doc(alias = "BString")]
pub struct ByteString(pub Vec<u8>);
impl ByteString {
#[inline]
pub(crate) fn as_bytes(&self) -> &[u8] {
&self.0
}
#[inline]
pub(crate) fn as_bytestr(&self) -> &ByteStr {
ByteStr::new(&self.0)
}
#[inline]
pub(crate) fn as_mut_bytestr(&mut self) -> &mut ByteStr {
ByteStr::from_bytes_mut(&mut self.0)
}
}
#[unstable(feature = "bstr", issue = "134915")]
impl Deref for ByteString {
type Target = Vec<u8>;
#[inline]
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[unstable(feature = "bstr", issue = "134915")]
impl DerefMut for ByteString {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
#[unstable(feature = "deref_pure_trait", issue = "87121")]
unsafe impl DerefPure for ByteString {}
#[unstable(feature = "bstr", issue = "134915")]
impl fmt::Debug for ByteString {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(self.as_bytestr(), f)
}
}
#[unstable(feature = "bstr", issue = "134915")]
impl fmt::Display for ByteString {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self.as_bytestr(), f)
}
}
#[unstable(feature = "bstr", issue = "134915")]
impl AsRef<[u8]> for ByteString {
#[inline]
fn as_ref(&self) -> &[u8] {
&self.0
}
}
#[unstable(feature = "bstr", issue = "134915")]
impl AsRef<ByteStr> for ByteString {
#[inline]
fn as_ref(&self) -> &ByteStr {
self.as_bytestr()
}
}
#[unstable(feature = "bstr", issue = "134915")]
impl AsMut<[u8]> for ByteString {
#[inline]
fn as_mut(&mut self) -> &mut [u8] {
&mut self.0
}
}
#[unstable(feature = "bstr", issue = "134915")]
impl AsMut<ByteStr> for ByteString {
#[inline]
fn as_mut(&mut self) -> &mut ByteStr {
self.as_mut_bytestr()
}
}
#[unstable(feature = "bstr", issue = "134915")]
impl Borrow<[u8]> for ByteString {
#[inline]
fn borrow(&self) -> &[u8] {
&self.0
}
}
#[unstable(feature = "bstr", issue = "134915")]
impl Borrow<ByteStr> for ByteString {
#[inline]
fn borrow(&self) -> &ByteStr {
self.as_bytestr()
}
}
// `impl Borrow<ByteStr> for Vec<u8>` omitted to avoid inference failures
// `impl Borrow<ByteStr> for String` omitted to avoid inference failures
#[unstable(feature = "bstr", issue = "134915")]
impl BorrowMut<[u8]> for ByteString {
#[inline]
fn borrow_mut(&mut self) -> &mut [u8] {
&mut self.0
}
}
#[unstable(feature = "bstr", issue = "134915")]
impl BorrowMut<ByteStr> for ByteString {
#[inline]
fn borrow_mut(&mut self) -> &mut ByteStr {
self.as_mut_bytestr()
}
}
// `impl BorrowMut<ByteStr> for Vec<u8>` omitted to avoid inference failures
#[unstable(feature = "bstr", issue = "134915")]
impl Default for ByteString {
fn default() -> Self {
ByteString(Vec::new())
}
}
// Omitted due to inference failures
//
// #[unstable(feature = "bstr", issue = "134915")]
// impl<'a, const N: usize> From<&'a [u8; N]> for ByteString {
// #[inline]
// fn from(s: &'a [u8; N]) -> Self {
// ByteString(s.as_slice().to_vec())
// }
// }
//
// #[unstable(feature = "bstr", issue = "134915")]
// impl<const N: usize> From<[u8; N]> for ByteString {
// #[inline]
// fn from(s: [u8; N]) -> Self {
// ByteString(s.as_slice().to_vec())
// }
// }
//
// #[unstable(feature = "bstr", issue = "134915")]
// impl<'a> From<&'a [u8]> for ByteString {
// #[inline]
// fn from(s: &'a [u8]) -> Self {
// ByteString(s.to_vec())
// }
// }
//
// #[unstable(feature = "bstr", issue = "134915")]
// impl From<Vec<u8>> for ByteString {
// /// Make a `ByteString` with `Vec<u8>` as inner
// #[inline]
// fn from(s: Vec<u8>) -> Self {
// ByteString(s)
// }
// }
#[unstable(feature = "bstr", issue = "134915")]
impl From<ByteString> for Vec<u8> {
#[inline]
fn from(s: ByteString) -> Self {
s.0
}
}
// Omitted due to inference failures
//
// #[unstable(feature = "bstr", issue = "134915")]
// impl<'a> From<&'a str> for ByteString {
// /// Allocate a new `ByteString` with a copy of the bytes in the slice.
// #[inline]
// fn from(s: &'a str) -> Self {
// ByteString(s.as_bytes().to_vec())
// }
// }
//
// #[unstable(feature = "bstr", issue = "134915")]
// impl From<String> for ByteString {
// #[inline]
// fn from(s: String) -> Self {
// ByteString(s.into_bytes())
// }
// }
#[unstable(feature = "bstr", issue = "134915")]
impl<'a> From<&'a ByteStr> for ByteString {
/// Allocates a `ByteString` containing the bytes of `ByteStr`.
#[inline]
fn from(s: &'a ByteStr) -> Self {
ByteString(s.0.to_vec())
}
}
#[unstable(feature = "bstr", issue = "134915")]
impl<'a> From<ByteString> for Cow<'a, ByteStr> {
/// Wrap `ByteString` in `Cow::Owned`.
#[inline]
fn from(s: ByteString) -> Self {
Cow::Owned(s)
}
}
#[unstable(feature = "bstr", issue = "134915")]
impl<'a> From<&'a ByteString> for Cow<'a, ByteStr> {
/// Wrap `ByteString` as byte str in `Cow::Borrowed`.
#[inline]
fn from(s: &'a ByteString) -> Self {
Cow::Borrowed(s.as_bytestr())
}
}
#[unstable(feature = "bstr", issue = "134915")]
impl FromIterator<char> for ByteString {
#[inline]
fn from_iter<T: IntoIterator<Item = char>>(iter: T) -> Self {
ByteString(iter.into_iter().collect::<String>().into_bytes())
}
}
#[unstable(feature = "bstr", issue = "134915")]
impl FromIterator<u8> for ByteString {
#[inline]
fn from_iter<T: IntoIterator<Item = u8>>(iter: T) -> Self {
ByteString(iter.into_iter().collect())
}
}
#[unstable(feature = "bstr", issue = "134915")]
impl<'a> FromIterator<&'a str> for ByteString {
#[inline]
fn from_iter<T: IntoIterator<Item = &'a str>>(iter: T) -> Self {
ByteString(iter.into_iter().collect::<String>().into_bytes())
}
}
#[unstable(feature = "bstr", issue = "134915")]
impl<'a> FromIterator<&'a [u8]> for ByteString {
#[inline]
fn from_iter<T: IntoIterator<Item = &'a [u8]>>(iter: T) -> Self {
let mut buf = Vec::new();
for b in iter {
buf.extend_from_slice(b);
}
ByteString(buf)
}
}
#[unstable(feature = "bstr", issue = "134915")]
impl<'a> FromIterator<&'a ByteStr> for ByteString {
#[inline]
fn from_iter<T: IntoIterator<Item = &'a ByteStr>>(iter: T) -> Self {
let mut buf = Vec::new();
for b in iter {
buf.extend_from_slice(&b.0);
}
ByteString(buf)
}
}
#[unstable(feature = "bstr", issue = "134915")]
impl FromIterator<ByteString> for ByteString {
#[inline]
fn from_iter<T: IntoIterator<Item = ByteString>>(iter: T) -> Self {
let mut buf = Vec::new();
for mut b in iter {
buf.append(&mut b.0);
}
ByteString(buf)
}
}
#[unstable(feature = "bstr", issue = "134915")]
impl FromStr for ByteString {
type Err = core::convert::Infallible;
#[inline]
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(ByteString(s.as_bytes().to_vec()))
}
}
#[unstable(feature = "bstr", issue = "134915")]
impl Index<usize> for ByteString {
type Output = u8;
#[inline]
fn index(&self, idx: usize) -> &u8 {
&self.0[idx]
}
}
#[unstable(feature = "bstr", issue = "134915")]
impl Index<RangeFull> for ByteString {
type Output = ByteStr;
#[inline]
fn index(&self, _: RangeFull) -> &ByteStr {
self.as_bytestr()
}
}
#[unstable(feature = "bstr", issue = "134915")]
impl Index<Range<usize>> for ByteString {
type Output = ByteStr;
#[inline]
fn index(&self, r: Range<usize>) -> &ByteStr {
ByteStr::from_bytes(&self.0[r])
}
}
#[unstable(feature = "bstr", issue = "134915")]
impl Index<RangeInclusive<usize>> for ByteString {
type Output = ByteStr;
#[inline]
fn index(&self, r: RangeInclusive<usize>) -> &ByteStr {
ByteStr::from_bytes(&self.0[r])
}
}
#[unstable(feature = "bstr", issue = "134915")]
impl Index<RangeFrom<usize>> for ByteString {
type Output = ByteStr;
#[inline]
fn index(&self, r: RangeFrom<usize>) -> &ByteStr {
ByteStr::from_bytes(&self.0[r])
}
}
#[unstable(feature = "bstr", issue = "134915")]
impl Index<RangeTo<usize>> for ByteString {
type Output = ByteStr;
#[inline]
fn index(&self, r: RangeTo<usize>) -> &ByteStr {
ByteStr::from_bytes(&self.0[r])
}
}
#[unstable(feature = "bstr", issue = "134915")]
impl Index<RangeToInclusive<usize>> for ByteString {
type Output = ByteStr;
#[inline]
fn index(&self, r: RangeToInclusive<usize>) -> &ByteStr {
ByteStr::from_bytes(&self.0[r])
}
}
#[unstable(feature = "bstr", issue = "134915")]
impl IndexMut<usize> for ByteString {
#[inline]
fn index_mut(&mut self, idx: usize) -> &mut u8 {
&mut self.0[idx]
}
}
#[unstable(feature = "bstr", issue = "134915")]
impl IndexMut<RangeFull> for ByteString {
#[inline]
fn index_mut(&mut self, _: RangeFull) -> &mut ByteStr {
self.as_mut_bytestr()
}
}
#[unstable(feature = "bstr", issue = "134915")]
impl IndexMut<Range<usize>> for ByteString {
#[inline]
fn index_mut(&mut self, r: Range<usize>) -> &mut ByteStr {
ByteStr::from_bytes_mut(&mut self.0[r])
}
}
#[unstable(feature = "bstr", issue = "134915")]
impl IndexMut<RangeInclusive<usize>> for ByteString {
#[inline]
fn index_mut(&mut self, r: RangeInclusive<usize>) -> &mut ByteStr {
ByteStr::from_bytes_mut(&mut self.0[r])
}
}
#[unstable(feature = "bstr", issue = "134915")]
impl IndexMut<RangeFrom<usize>> for ByteString {
#[inline]
fn index_mut(&mut self, r: RangeFrom<usize>) -> &mut ByteStr {
ByteStr::from_bytes_mut(&mut self.0[r])
}
}
#[unstable(feature = "bstr", issue = "134915")]
impl IndexMut<RangeTo<usize>> for ByteString {
#[inline]
fn index_mut(&mut self, r: RangeTo<usize>) -> &mut ByteStr {
ByteStr::from_bytes_mut(&mut self.0[r])
}
}
#[unstable(feature = "bstr", issue = "134915")]
impl IndexMut<RangeToInclusive<usize>> for ByteString {
#[inline]
fn index_mut(&mut self, r: RangeToInclusive<usize>) -> &mut ByteStr {
ByteStr::from_bytes_mut(&mut self.0[r])
}
}
#[unstable(feature = "bstr", issue = "134915")]
impl hash::Hash for ByteString {
#[inline]
fn hash<H: hash::Hasher>(&self, state: &mut H) {
self.0.hash(state);
}
}
#[unstable(feature = "bstr", issue = "134915")]
impl Eq for ByteString {}
#[unstable(feature = "bstr", issue = "134915")]
impl PartialEq for ByteString {
#[inline]
fn eq(&self, other: &ByteString) -> bool {
self.0 == other.0
}
}
macro_rules! impl_partial_eq_ord_cow {
($lhs:ty, $rhs:ty) => {
#[allow(unused_lifetimes)]
#[unstable(feature = "bstr", issue = "134915")]
impl<'a> PartialEq<$rhs> for $lhs {
#[inline]
fn eq(&self, other: &$rhs) -> bool {
let other: &[u8] = (&**other).as_ref();
PartialEq::eq(self.as_bytes(), other)
}
}
#[allow(unused_lifetimes)]
#[unstable(feature = "bstr", issue = "134915")]
impl<'a> PartialEq<$lhs> for $rhs {
#[inline]
fn eq(&self, other: &$lhs) -> bool {
let this: &[u8] = (&**self).as_ref();
PartialEq::eq(this, other.as_bytes())
}
}
#[allow(unused_lifetimes)]
#[unstable(feature = "bstr", issue = "134915")]
impl<'a> PartialOrd<$rhs> for $lhs {
#[inline]
fn partial_cmp(&self, other: &$rhs) -> Option<Ordering> {
let other: &[u8] = (&**other).as_ref();
PartialOrd::partial_cmp(self.as_bytes(), other)
}
}
#[allow(unused_lifetimes)]
#[unstable(feature = "bstr", issue = "134915")]
impl<'a> PartialOrd<$lhs> for $rhs {
#[inline]
fn partial_cmp(&self, other: &$lhs) -> Option<Ordering> {
let this: &[u8] = (&**self).as_ref();
PartialOrd::partial_cmp(this, other.as_bytes())
}
}
};
}
// PartialOrd with `Vec<u8>` omitted to avoid inference failures
impl_partial_eq!(ByteString, Vec<u8>);
// PartialOrd with `[u8]` omitted to avoid inference failures
impl_partial_eq!(ByteString, [u8]);
// PartialOrd with `&[u8]` omitted to avoid inference failures
impl_partial_eq!(ByteString, &[u8]);
// PartialOrd with `String` omitted to avoid inference failures
impl_partial_eq!(ByteString, String);
// PartialOrd with `str` omitted to avoid inference failures
impl_partial_eq!(ByteString, str);
// PartialOrd with `&str` omitted to avoid inference failures
impl_partial_eq!(ByteString, &str);
impl_partial_eq_ord!(ByteString, ByteStr);
impl_partial_eq_ord!(ByteString, &ByteStr);
// PartialOrd with `[u8; N]` omitted to avoid inference failures
impl_partial_eq_n!(ByteString, [u8; N]);
// PartialOrd with `&[u8; N]` omitted to avoid inference failures
impl_partial_eq_n!(ByteString, &[u8; N]);
impl_partial_eq_ord_cow!(ByteString, Cow<'_, ByteStr>);
impl_partial_eq_ord_cow!(ByteString, Cow<'_, str>);
impl_partial_eq_ord_cow!(ByteString, Cow<'_, [u8]>);
#[unstable(feature = "bstr", issue = "134915")]
impl Ord for ByteString {
#[inline]
fn cmp(&self, other: &ByteString) -> Ordering {
Ord::cmp(&self.0, &other.0)
}
}
#[unstable(feature = "bstr", issue = "134915")]
impl PartialOrd for ByteString {
#[inline]
fn partial_cmp(&self, other: &ByteString) -> Option<Ordering> {
PartialOrd::partial_cmp(&self.0, &other.0)
}
}
#[unstable(feature = "bstr", issue = "134915")]
impl ToOwned for ByteStr {
type Owned = ByteString;
#[inline]
fn to_owned(&self) -> ByteString {
ByteString(self.0.to_vec())
}
}
#[unstable(feature = "bstr", issue = "134915")]
impl TryFrom<ByteString> for String {
type Error = crate::string::FromUtf8Error;
#[inline]
fn try_from(s: ByteString) -> Result<Self, Self::Error> {
String::from_utf8(s.0)
}
}
#[unstable(feature = "bstr", issue = "134915")]
impl<'a> TryFrom<&'a ByteString> for &'a str {
type Error = crate::str::Utf8Error;
#[inline]
fn try_from(s: &'a ByteString) -> Result<Self, Self::Error> {
crate::str::from_utf8(s.0.as_slice())
}
}
// Additional impls for `ByteStr` that require types from `alloc`:
#[unstable(feature = "bstr", issue = "134915")]
impl Clone for Box<ByteStr> {
#[inline]
fn clone(&self) -> Self {
Self::from(Box::<[u8]>::from(&self.0))
}
}
#[unstable(feature = "bstr", issue = "134915")]
impl<'a> From<&'a ByteStr> for Cow<'a, ByteStr> {
/// Wrap `ByteStr` in `Cow::Borrowed`.
#[inline]
fn from(s: &'a ByteStr) -> Self {
Cow::Borrowed(s)
}
}
#[unstable(feature = "bstr", issue = "134915")]
impl From<Box<[u8]>> for Box<ByteStr> {
/// Move the bytes from `Box<ByteStr>` to `Box<[u8]>`, this does not allocate new memory.
#[inline]
fn from(s: Box<[u8]>) -> Box<ByteStr> {
// SAFETY: `ByteStr` is a transparent wrapper around `[u8]`.
unsafe { Box::from_raw(Box::into_raw(s) as _) }
}
}
#[unstable(feature = "bstr", issue = "134915")]
impl From<Box<ByteStr>> for Box<[u8]> {
/// Convert the inner bytes of `Box<[u8]>` to `ByteStr`.
#[inline]
fn from(s: Box<ByteStr>) -> Box<[u8]> {
// SAFETY: `ByteStr` is a transparent wrapper around `[u8]`.
unsafe { Box::from_raw(Box::into_raw(s) as _) }
}
}
#[unstable(feature = "bstr", issue = "134915")]
#[cfg(not(no_rc))]
impl From<Rc<[u8]>> for Rc<ByteStr> {
/// Create an `Rc<[u8]>` from the inner bytes of `Rc<ByteStr>`.
#[inline]
fn from(s: Rc<[u8]>) -> Rc<ByteStr> {
// SAFETY: `ByteStr` is a transparent wrapper around `[u8]`.
unsafe { Rc::from_raw(Rc::into_raw(s) as _) }
}
}
#[unstable(feature = "bstr", issue = "134915")]
#[cfg(not(no_rc))]
impl From<Rc<ByteStr>> for Rc<[u8]> {
/// Create a `Rc<ByteStr>` from the bytes of `Rc<[u8]>`.
#[inline]
fn from(s: Rc<ByteStr>) -> Rc<[u8]> {
// SAFETY: `ByteStr` is a transparent wrapper around `[u8]`.
unsafe { Rc::from_raw(Rc::into_raw(s) as _) }
}
}
#[unstable(feature = "bstr", issue = "134915")]
#[cfg(all(not(no_rc), not(no_sync), target_has_atomic = "ptr"))]
impl From<Arc<[u8]>> for Arc<ByteStr> {
/// Create an `Arc<ByteStr>` from the bytes of `Arc<[u8]>`.
#[inline]
fn from(s: Arc<[u8]>) -> Arc<ByteStr> {
// SAFETY: `ByteStr` is a transparent wrapper around `[u8]`.
unsafe { Arc::from_raw(Arc::into_raw(s) as _) }
}
}
#[unstable(feature = "bstr", issue = "134915")]
#[cfg(all(not(no_rc), not(no_sync), target_has_atomic = "ptr"))]
impl From<Arc<ByteStr>> for Arc<[u8]> {
/// Create a `Arc<[u8]>` from the inner bytes of `Arc<ByteStr>`.
#[inline]
fn from(s: Arc<ByteStr>) -> Arc<[u8]> {
// SAFETY: `ByteStr` is a transparent wrapper around `[u8]`.
unsafe { Arc::from_raw(Arc::into_raw(s) as _) }
}
}
// PartialOrd with `Vec<u8>` omitted to avoid inference failures
impl_partial_eq!(ByteStr, Vec<u8>);
// PartialOrd with `String` omitted to avoid inference failures
impl_partial_eq!(ByteStr, String);
impl_partial_eq_ord_cow!(&'a ByteStr, Cow<'a, ByteStr>);
impl_partial_eq_ord_cow!(&'a ByteStr, Cow<'a, str>);
impl_partial_eq_ord_cow!(&'a ByteStr, Cow<'a, [u8]>);
#[unstable(feature = "bstr", issue = "134915")]
impl<'a> TryFrom<&'a ByteStr> for String {
type Error = core::str::Utf8Error;
/// Try to allocate a `ByteStr`s bytes as a UTF-8 `String`.
///
/// # Errors
/// If `ByteStr` is not valid UTF-8
#[inline]
fn try_from(s: &'a ByteStr) -> Result<Self, Self::Error> {
Ok(core::str::from_utf8(&s.0)?.into())
}
}