ntex_bytes/
bvec.rs

1use std::borrow::{Borrow, BorrowMut};
2use std::ops::{Deref, DerefMut};
3use std::{fmt, ptr};
4
5use crate::storage::StorageVec;
6use crate::{buf::IntoIter, buf::UninitSlice, debug, Buf, BufMut, Bytes, BytesMut};
7
8/// A unique reference to a contiguous slice of memory.
9///
10/// `BytesVec` represents a unique view into a potentially shared memory region.
11/// Given the uniqueness guarantee, owners of `BytesVec` handles are able to
12/// mutate the memory. It is similar to a `Vec<u8>` but with less copies and
13/// allocations. It also always allocates.
14///
15/// For more detail, see [Bytes](struct.Bytes.html).
16///
17/// # Growth
18///
19/// One key difference from `Vec<u8>` is that most operations **do not
20/// implicitly grow the buffer**. This means that calling `my_bytes.put("hello
21/// world");` could panic if `my_bytes` does not have enough capacity. Before
22/// writing to the buffer, ensure that there is enough remaining capacity by
23/// calling `my_bytes.remaining_mut()`. In general, avoiding calls to `reserve`
24/// is preferable.
25///
26/// The only exception is `extend` which implicitly reserves required capacity.
27///
28/// # Examples
29///
30/// ```
31/// use ntex_bytes::{BytesVec, BufMut};
32///
33/// let mut buf = BytesVec::with_capacity(64);
34///
35/// buf.put_u8(b'h');
36/// buf.put_u8(b'e');
37/// buf.put("llo");
38///
39/// assert_eq!(&buf[..], b"hello");
40///
41/// // Freeze the buffer so that it can be shared
42/// let a = buf.freeze();
43///
44/// // This does not allocate, instead `b` points to the same memory.
45/// let b = a.clone();
46///
47/// assert_eq!(a, b"hello");
48/// assert_eq!(b, b"hello");
49/// ```
50pub struct BytesVec {
51    pub(crate) inner: StorageVec,
52}
53
54/*
55 *
56 * ===== BytesVec =====
57 *
58 */
59
60impl BytesVec {
61    /// Creates a new `BytesVec` with the specified capacity.
62    ///
63    /// The returned `BytesVec` will be able to hold at least `capacity` bytes
64    /// without reallocating.
65    ///
66    /// It is important to note that this function does not specify the length
67    /// of the returned `BytesVec`, but only the capacity.
68    ///
69    /// # Panics
70    ///
71    /// Panics if `capacity` greater than 60bit for 64bit systems
72    /// and 28bit for 32bit systems
73    ///
74    /// # Examples
75    ///
76    /// ```
77    /// use ntex_bytes::{BytesVec, BufMut};
78    ///
79    /// let mut bytes = BytesVec::with_capacity(64);
80    ///
81    /// // `bytes` contains no data, even though there is capacity
82    /// assert_eq!(bytes.len(), 0);
83    ///
84    /// bytes.put(&b"hello world"[..]);
85    ///
86    /// assert_eq!(&bytes[..], b"hello world");
87    /// ```
88    #[inline]
89    pub fn with_capacity(capacity: usize) -> BytesVec {
90        BytesVec {
91            inner: StorageVec::with_capacity(capacity),
92        }
93    }
94
95    /// Creates a new `BytesVec` from slice, by copying it.
96    pub fn copy_from_slice<T: AsRef<[u8]>>(src: T) -> Self {
97        BytesVec {
98            inner: StorageVec::from_slice(src.as_ref()),
99        }
100    }
101
102    /// Creates a new `BytesVec` with default capacity.
103    ///
104    /// Resulting object has length 0 and unspecified capacity.
105    /// This function does not allocate.
106    ///
107    /// # Examples
108    ///
109    /// ```
110    /// use ntex_bytes::{BytesVec, BufMut};
111    ///
112    /// let mut bytes = BytesVec::new();
113    ///
114    /// assert_eq!(0, bytes.len());
115    ///
116    /// bytes.reserve(2);
117    /// bytes.put_slice(b"xy");
118    ///
119    /// assert_eq!(&b"xy"[..], &bytes[..]);
120    /// ```
121    #[inline]
122    pub fn new() -> BytesVec {
123        BytesVec {
124            inner: StorageVec::with_capacity(0),
125        }
126    }
127
128    /// Returns the number of bytes contained in this `BytesVec`.
129    ///
130    /// # Examples
131    ///
132    /// ```
133    /// use ntex_bytes::BytesVec;
134    ///
135    /// let b = BytesVec::copy_from_slice(&b"hello"[..]);
136    /// assert_eq!(b.len(), 5);
137    /// ```
138    #[inline]
139    pub fn len(&self) -> usize {
140        self.inner.len()
141    }
142
143    /// Returns true if the `BytesVec` has a length of 0.
144    ///
145    /// # Examples
146    ///
147    /// ```
148    /// use ntex_bytes::BytesVec;
149    ///
150    /// let b = BytesVec::with_capacity(64);
151    /// assert!(b.is_empty());
152    /// ```
153    #[inline]
154    pub fn is_empty(&self) -> bool {
155        self.inner.len() == 0
156    }
157
158    /// Returns the number of bytes the `BytesVec` can hold without reallocating.
159    ///
160    /// # Examples
161    ///
162    /// ```
163    /// use ntex_bytes::BytesVec;
164    ///
165    /// let b = BytesVec::with_capacity(64);
166    /// assert_eq!(b.capacity(), 72);
167    /// ```
168    #[inline]
169    pub fn capacity(&self) -> usize {
170        self.inner.capacity()
171    }
172
173    /// Converts `self` into an immutable `Bytes`.
174    ///
175    /// The conversion is zero cost and is used to indicate that the slice
176    /// referenced by the handle will no longer be mutated. Once the conversion
177    /// is done, the handle can be cloned and shared across threads.
178    ///
179    /// # Examples
180    ///
181    /// ```
182    /// use ntex_bytes::{BytesVec, BufMut};
183    /// use std::thread;
184    ///
185    /// let mut b = BytesVec::with_capacity(64);
186    /// b.put("hello world");
187    /// let b1 = b.freeze();
188    /// let b2 = b1.clone();
189    ///
190    /// let th = thread::spawn(move || {
191    ///     assert_eq!(b1, b"hello world");
192    /// });
193    ///
194    /// assert_eq!(b2, b"hello world");
195    /// th.join().unwrap();
196    /// ```
197    #[inline]
198    pub fn freeze(self) -> Bytes {
199        Bytes {
200            inner: self.inner.freeze(),
201        }
202    }
203
204    /// Removes the bytes from the current view, returning them in a new
205    /// `Bytes` instance.
206    ///
207    /// Afterwards, `self` will be empty, but will retain any additional
208    /// capacity that it had before the operation. This is identical to
209    /// `self.split_to(self.len())`.
210    ///
211    /// This is an `O(1)` operation that just increases the reference count and
212    /// sets a few indices.
213    ///
214    /// # Examples
215    ///
216    /// ```
217    /// use ntex_bytes::{BytesVec, BufMut};
218    ///
219    /// let mut buf = BytesVec::with_capacity(1024);
220    /// buf.put(&b"hello world"[..]);
221    ///
222    /// let other = buf.split();
223    ///
224    /// assert!(buf.is_empty());
225    /// assert_eq!(1021, buf.capacity());
226    ///
227    /// assert_eq!(other, b"hello world"[..]);
228    /// ```
229    pub fn split(&mut self) -> BytesMut {
230        self.split_to(self.len())
231    }
232
233    /// Splits the buffer into two at the given index.
234    ///
235    /// Afterwards `self` contains elements `[at, len)`, and the returned `Bytes`
236    /// contains elements `[0, at)`.
237    ///
238    /// This is an `O(1)` operation that just increases the reference count and
239    /// sets a few indices.
240    ///
241    /// # Examples
242    ///
243    /// ```
244    /// use ntex_bytes::BytesVec;
245    ///
246    /// let mut a = BytesVec::copy_from_slice(&b"hello world"[..]);
247    /// let mut b = a.split_to(5);
248    ///
249    /// a[0] = b'!';
250    ///
251    /// assert_eq!(&a[..], b"!world");
252    /// assert_eq!(&b[..], b"hello");
253    /// ```
254    ///
255    /// # Panics
256    ///
257    /// Panics if `at > len`.
258    pub fn split_to(&mut self, at: usize) -> BytesMut {
259        self.split_to_checked(at)
260            .expect("at value must be <= self.len()`")
261    }
262
263    /// Splits the bytes into two at the given index.
264    ///
265    /// Does nothing if `at > len`.
266    pub fn split_to_checked(&mut self, at: usize) -> Option<BytesMut> {
267        if at <= self.len() {
268            Some(BytesMut {
269                inner: self.inner.split_to(at, false),
270            })
271        } else {
272            None
273        }
274    }
275
276    /// Shortens the buffer, keeping the first `len` bytes and dropping the
277    /// rest.
278    ///
279    /// If `len` is greater than the buffer's current length, this has no
280    /// effect.
281    ///
282    /// The [`split_off`] method can emulate `truncate`, but this causes the
283    /// excess bytes to be returned instead of dropped.
284    ///
285    /// # Examples
286    ///
287    /// ```
288    /// use ntex_bytes::BytesVec;
289    ///
290    /// let mut buf = BytesVec::copy_from_slice(&b"hello world"[..]);
291    /// buf.truncate(5);
292    /// assert_eq!(buf, b"hello"[..]);
293    /// ```
294    ///
295    /// [`split_off`]: #method.split_off
296    pub fn truncate(&mut self, len: usize) {
297        self.inner.truncate(len);
298    }
299
300    /// Clears the buffer, removing all data.
301    ///
302    /// # Examples
303    ///
304    /// ```
305    /// use ntex_bytes::BytesVec;
306    ///
307    /// let mut buf = BytesVec::copy_from_slice(&b"hello world"[..]);
308    /// buf.clear();
309    /// assert!(buf.is_empty());
310    /// ```
311    pub fn clear(&mut self) {
312        self.truncate(0);
313    }
314
315    /// Resizes the buffer so that `len` is equal to `new_len`.
316    ///
317    /// If `new_len` is greater than `len`, the buffer is extended by the
318    /// difference with each additional byte set to `value`. If `new_len` is
319    /// less than `len`, the buffer is simply truncated.
320    ///
321    /// # Panics
322    ///
323    /// Panics if `new_len` greater than 60bit for 64bit systems
324    /// and 28bit for 32bit systems
325    ///
326    /// # Examples
327    ///
328    /// ```
329    /// use ntex_bytes::BytesVec;
330    ///
331    /// let mut buf = BytesVec::new();
332    ///
333    /// buf.resize(3, 0x1);
334    /// assert_eq!(&buf[..], &[0x1, 0x1, 0x1]);
335    ///
336    /// buf.resize(2, 0x2);
337    /// assert_eq!(&buf[..], &[0x1, 0x1]);
338    ///
339    /// buf.resize(4, 0x3);
340    /// assert_eq!(&buf[..], &[0x1, 0x1, 0x3, 0x3]);
341    /// ```
342    #[inline]
343    pub fn resize(&mut self, new_len: usize, value: u8) {
344        self.inner.resize(new_len, value);
345    }
346
347    /// Sets the length of the buffer.
348    ///
349    /// This will explicitly set the size of the buffer without actually
350    /// modifying the data, so it is up to the caller to ensure that the data
351    /// has been initialized.
352    ///
353    /// # Examples
354    ///
355    /// ```
356    /// use ntex_bytes::BytesVec;
357    ///
358    /// let mut b = BytesVec::copy_from_slice(&b"hello world"[..]);
359    ///
360    /// unsafe {
361    ///     b.set_len(5);
362    /// }
363    ///
364    /// assert_eq!(&b[..], b"hello");
365    ///
366    /// unsafe {
367    ///     b.set_len(11);
368    /// }
369    ///
370    /// assert_eq!(&b[..], b"hello world");
371    /// ```
372    ///
373    /// # Panics
374    ///
375    /// This method will panic if `len` is out of bounds for the underlying
376    /// slice or if it comes after the `end` of the configured window.
377    #[inline]
378    #[allow(clippy::missing_safety_doc)]
379    pub unsafe fn set_len(&mut self, len: usize) {
380        self.inner.set_len(len)
381    }
382
383    /// Reserves capacity for at least `additional` more bytes to be inserted
384    /// into the given `BytesVec`.
385    ///
386    /// More than `additional` bytes may be reserved in order to avoid frequent
387    /// reallocations. A call to `reserve` may result in an allocation.
388    ///
389    /// Before allocating new buffer space, the function will attempt to reclaim
390    /// space in the existing buffer. If the current handle references a small
391    /// view in the original buffer and all other handles have been dropped,
392    /// and the requested capacity is less than or equal to the existing
393    /// buffer's capacity, then the current view will be copied to the front of
394    /// the buffer and the handle will take ownership of the full buffer.
395    ///
396    /// # Panics
397    ///
398    /// Panics if new capacity is greater than 60bit for 64bit systems
399    /// and 28bit for 32bit systems
400    ///
401    /// # Examples
402    ///
403    /// In the following example, a new buffer is allocated.
404    ///
405    /// ```
406    /// use ntex_bytes::BytesVec;
407    ///
408    /// let mut buf = BytesVec::copy_from_slice(&b"hello"[..]);
409    /// buf.reserve(64);
410    /// assert!(buf.capacity() >= 69);
411    /// ```
412    ///
413    /// In the following example, the existing buffer is reclaimed.
414    ///
415    /// ```
416    /// use ntex_bytes::{BytesVec, BufMut};
417    ///
418    /// let mut buf = BytesVec::with_capacity(128);
419    /// buf.put(&[0; 64][..]);
420    ///
421    /// let ptr = buf.as_ptr();
422    /// let other = buf.split();
423    ///
424    /// assert!(buf.is_empty());
425    /// assert_eq!(buf.capacity(), 80);
426    ///
427    /// drop(other);
428    /// buf.reserve(128);
429    ///
430    /// assert_eq!(buf.capacity(), 144);
431    /// assert_eq!(buf.as_ptr(), ptr);
432    /// ```
433    ///
434    /// # Panics
435    ///
436    /// Panics if the new capacity overflows `usize`.
437    #[inline]
438    pub fn reserve(&mut self, additional: usize) {
439        self.inner.reserve(additional);
440    }
441
442    /// Appends given bytes to this object.
443    ///
444    /// If this `BytesVec` object has not enough capacity, it is resized first.
445    /// So unlike `put_slice` operation, `extend_from_slice` does not panic.
446    ///
447    /// # Examples
448    ///
449    /// ```
450    /// use ntex_bytes::BytesVec;
451    ///
452    /// let mut buf = BytesVec::with_capacity(0);
453    /// buf.extend_from_slice(b"aaabbb");
454    /// buf.extend_from_slice(b"cccddd");
455    ///
456    /// assert_eq!(b"aaabbbcccddd", &buf[..]);
457    /// ```
458    #[inline]
459    pub fn extend_from_slice(&mut self, extend: &[u8]) {
460        self.put_slice(extend);
461    }
462
463    /// Run provided function with `BytesMut` instance that contains current data.
464    #[inline]
465    pub fn with_bytes_mut<F, R>(&mut self, f: F) -> R
466    where
467        F: FnOnce(&mut BytesMut) -> R,
468    {
469        self.inner.with_bytes_mut(f)
470    }
471
472    /// Returns an iterator over the bytes contained by the buffer.
473    ///
474    /// # Examples
475    ///
476    /// ```
477    /// use ntex_bytes::{Buf, BytesVec};
478    ///
479    /// let buf = BytesVec::copy_from_slice(&b"abc"[..]);
480    /// let mut iter = buf.iter();
481    ///
482    /// assert_eq!(iter.next().map(|b| *b), Some(b'a'));
483    /// assert_eq!(iter.next().map(|b| *b), Some(b'b'));
484    /// assert_eq!(iter.next().map(|b| *b), Some(b'c'));
485    /// assert_eq!(iter.next(), None);
486    /// ```
487    #[inline]
488    pub fn iter(&'_ self) -> std::slice::Iter<'_, u8> {
489        self.chunk().iter()
490    }
491}
492
493impl Buf for BytesVec {
494    #[inline]
495    fn remaining(&self) -> usize {
496        self.len()
497    }
498
499    #[inline]
500    fn chunk(&self) -> &[u8] {
501        self.inner.as_ref()
502    }
503
504    #[inline]
505    fn advance(&mut self, cnt: usize) {
506        assert!(
507            cnt <= self.inner.len(),
508            "cannot advance past `remaining` len:{} delta:{}",
509            self.inner.len(),
510            cnt
511        );
512        unsafe {
513            self.inner.set_start(cnt as u32);
514        }
515    }
516}
517
518impl BufMut for BytesVec {
519    #[inline]
520    fn remaining_mut(&self) -> usize {
521        self.capacity() - self.len()
522    }
523
524    #[inline]
525    unsafe fn advance_mut(&mut self, cnt: usize) {
526        // This call will panic if `cnt` is too big
527        self.inner.set_len(self.len() + cnt);
528    }
529
530    #[inline]
531    fn chunk_mut(&mut self) -> &mut UninitSlice {
532        let len = self.len();
533
534        unsafe {
535            // This will never panic as `len` can never become invalid
536            let ptr = &mut self.inner.as_raw()[len..];
537            UninitSlice::from_raw_parts_mut(ptr.as_mut_ptr(), self.capacity() - len)
538        }
539    }
540
541    #[inline]
542    fn put_slice(&mut self, src: &[u8]) {
543        let len = src.len();
544        self.reserve(len);
545
546        unsafe {
547            ptr::copy_nonoverlapping(src.as_ptr(), self.chunk_mut().as_mut_ptr(), len);
548            self.advance_mut(len);
549        }
550    }
551
552    #[inline]
553    fn put_u8(&mut self, n: u8) {
554        self.reserve(1);
555        self.inner.put_u8(n);
556    }
557
558    #[inline]
559    fn put_i8(&mut self, n: i8) {
560        self.reserve(1);
561        self.put_u8(n as u8);
562    }
563}
564
565impl bytes::buf::Buf for BytesVec {
566    #[inline]
567    fn remaining(&self) -> usize {
568        self.len()
569    }
570
571    #[inline]
572    fn chunk(&self) -> &[u8] {
573        self.inner.as_ref()
574    }
575
576    #[inline]
577    fn advance(&mut self, cnt: usize) {
578        Buf::advance(self, cnt)
579    }
580}
581
582unsafe impl bytes::buf::BufMut for BytesVec {
583    #[inline]
584    fn remaining_mut(&self) -> usize {
585        BufMut::remaining_mut(self)
586    }
587
588    #[inline]
589    unsafe fn advance_mut(&mut self, cnt: usize) {
590        BufMut::advance_mut(self, cnt)
591    }
592
593    #[inline]
594    fn chunk_mut(&mut self) -> &mut bytes::buf::UninitSlice {
595        let len = self.len();
596        unsafe {
597            // This will never panic as `len` can never become invalid
598            let ptr = &mut self.inner.as_raw()[len..];
599            bytes::buf::UninitSlice::from_raw_parts_mut(
600                ptr.as_mut_ptr(),
601                self.capacity() - len,
602            )
603        }
604    }
605
606    #[inline]
607    fn put_slice(&mut self, src: &[u8]) {
608        BufMut::put_slice(self, src)
609    }
610
611    #[inline]
612    fn put_u8(&mut self, n: u8) {
613        BufMut::put_u8(self, n)
614    }
615
616    #[inline]
617    fn put_i8(&mut self, n: i8) {
618        BufMut::put_i8(self, n)
619    }
620}
621
622impl AsRef<[u8]> for BytesVec {
623    #[inline]
624    fn as_ref(&self) -> &[u8] {
625        self.inner.as_ref()
626    }
627}
628
629impl AsMut<[u8]> for BytesVec {
630    #[inline]
631    fn as_mut(&mut self) -> &mut [u8] {
632        self.inner.as_mut()
633    }
634}
635
636impl Deref for BytesVec {
637    type Target = [u8];
638
639    #[inline]
640    fn deref(&self) -> &[u8] {
641        self.as_ref()
642    }
643}
644
645impl DerefMut for BytesVec {
646    #[inline]
647    fn deref_mut(&mut self) -> &mut [u8] {
648        self.inner.as_mut()
649    }
650}
651
652impl Eq for BytesVec {}
653
654impl PartialEq for BytesVec {
655    #[inline]
656    fn eq(&self, other: &BytesVec) -> bool {
657        self.inner.as_ref() == other.inner.as_ref()
658    }
659}
660
661impl Default for BytesVec {
662    #[inline]
663    fn default() -> BytesVec {
664        BytesVec::new()
665    }
666}
667
668impl Borrow<[u8]> for BytesVec {
669    #[inline]
670    fn borrow(&self) -> &[u8] {
671        self.as_ref()
672    }
673}
674
675impl BorrowMut<[u8]> for BytesVec {
676    #[inline]
677    fn borrow_mut(&mut self) -> &mut [u8] {
678        self.as_mut()
679    }
680}
681
682impl PartialEq<Bytes> for BytesVec {
683    fn eq(&self, other: &Bytes) -> bool {
684        other[..] == self[..]
685    }
686}
687
688impl PartialEq<BytesMut> for BytesVec {
689    fn eq(&self, other: &BytesMut) -> bool {
690        other[..] == self[..]
691    }
692}
693
694impl fmt::Debug for BytesVec {
695    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
696        fmt::Debug::fmt(&debug::BsDebug(self.inner.as_ref()), fmt)
697    }
698}
699
700impl fmt::Write for BytesVec {
701    #[inline]
702    fn write_str(&mut self, s: &str) -> fmt::Result {
703        if self.remaining_mut() >= s.len() {
704            self.put_slice(s.as_bytes());
705            Ok(())
706        } else {
707            Err(fmt::Error)
708        }
709    }
710
711    #[inline]
712    fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> fmt::Result {
713        fmt::write(self, args)
714    }
715}
716
717impl IntoIterator for BytesVec {
718    type Item = u8;
719    type IntoIter = IntoIter<BytesVec>;
720
721    fn into_iter(self) -> Self::IntoIter {
722        IntoIter::new(self)
723    }
724}
725
726impl<'a> IntoIterator for &'a BytesVec {
727    type Item = &'a u8;
728    type IntoIter = std::slice::Iter<'a, u8>;
729
730    fn into_iter(self) -> Self::IntoIter {
731        self.as_ref().iter()
732    }
733}
734
735impl FromIterator<u8> for BytesVec {
736    fn from_iter<T: IntoIterator<Item = u8>>(into_iter: T) -> Self {
737        let iter = into_iter.into_iter();
738        let (min, maybe_max) = iter.size_hint();
739
740        let mut out = BytesVec::with_capacity(maybe_max.unwrap_or(min));
741        for i in iter {
742            out.reserve(1);
743            out.put_u8(i);
744        }
745
746        out
747    }
748}
749
750impl<'a> FromIterator<&'a u8> for BytesVec {
751    fn from_iter<T: IntoIterator<Item = &'a u8>>(into_iter: T) -> Self {
752        into_iter.into_iter().copied().collect::<BytesVec>()
753    }
754}
755
756impl Extend<u8> for BytesVec {
757    fn extend<T>(&mut self, iter: T)
758    where
759        T: IntoIterator<Item = u8>,
760    {
761        let iter = iter.into_iter();
762
763        let (lower, _) = iter.size_hint();
764        self.reserve(lower);
765
766        for b in iter {
767            self.put_u8(b);
768        }
769    }
770}
771
772impl<'a> Extend<&'a u8> for BytesVec {
773    fn extend<T>(&mut self, iter: T)
774    where
775        T: IntoIterator<Item = &'a u8>,
776    {
777        self.extend(iter.into_iter().copied())
778    }
779}
780
781impl PartialEq<[u8]> for BytesVec {
782    fn eq(&self, other: &[u8]) -> bool {
783        &**self == other
784    }
785}
786
787impl<const N: usize> PartialEq<[u8; N]> for BytesVec {
788    fn eq(&self, other: &[u8; N]) -> bool {
789        &**self == other
790    }
791}
792
793impl PartialEq<BytesVec> for [u8] {
794    fn eq(&self, other: &BytesVec) -> bool {
795        *other == *self
796    }
797}
798
799impl<const N: usize> PartialEq<BytesVec> for [u8; N] {
800    fn eq(&self, other: &BytesVec) -> bool {
801        *other == *self
802    }
803}
804
805impl<const N: usize> PartialEq<BytesVec> for &[u8; N] {
806    fn eq(&self, other: &BytesVec) -> bool {
807        *other == *self
808    }
809}
810
811impl PartialEq<str> for BytesVec {
812    fn eq(&self, other: &str) -> bool {
813        &**self == other.as_bytes()
814    }
815}
816
817impl PartialEq<BytesVec> for str {
818    fn eq(&self, other: &BytesVec) -> bool {
819        *other == *self
820    }
821}
822
823impl PartialEq<Vec<u8>> for BytesVec {
824    fn eq(&self, other: &Vec<u8>) -> bool {
825        *self == other[..]
826    }
827}
828
829impl PartialEq<BytesVec> for Vec<u8> {
830    fn eq(&self, other: &BytesVec) -> bool {
831        *other == *self
832    }
833}
834
835impl PartialEq<String> for BytesVec {
836    fn eq(&self, other: &String) -> bool {
837        *self == other[..]
838    }
839}
840
841impl PartialEq<BytesVec> for String {
842    fn eq(&self, other: &BytesVec) -> bool {
843        *other == *self
844    }
845}
846
847impl<'a, T: ?Sized> PartialEq<&'a T> for BytesVec
848where
849    BytesVec: PartialEq<T>,
850{
851    fn eq(&self, other: &&'a T) -> bool {
852        *self == **other
853    }
854}
855
856impl PartialEq<BytesVec> for &[u8] {
857    fn eq(&self, other: &BytesVec) -> bool {
858        *other == *self
859    }
860}
861
862impl PartialEq<BytesVec> for &str {
863    fn eq(&self, other: &BytesVec) -> bool {
864        *other == *self
865    }
866}
867
868impl PartialEq<BytesVec> for Bytes {
869    fn eq(&self, other: &BytesVec) -> bool {
870        other[..] == self[..]
871    }
872}
873
874impl PartialEq<BytesVec> for BytesMut {
875    fn eq(&self, other: &BytesVec) -> bool {
876        other[..] == self[..]
877    }
878}
879
880impl From<BytesVec> for Bytes {
881    #[inline]
882    fn from(b: BytesVec) -> Self {
883        b.freeze()
884    }
885}
886
887impl<'a> From<&'a [u8]> for BytesVec {
888    #[inline]
889    fn from(src: &'a [u8]) -> BytesVec {
890        BytesVec::copy_from_slice(src)
891    }
892}
893
894impl<const N: usize> From<[u8; N]> for BytesVec {
895    #[inline]
896    fn from(src: [u8; N]) -> BytesVec {
897        BytesVec::copy_from_slice(src)
898    }
899}
900
901impl<'a, const N: usize> From<&'a [u8; N]> for BytesVec {
902    #[inline]
903    fn from(src: &'a [u8; N]) -> BytesVec {
904        BytesVec::copy_from_slice(src)
905    }
906}
907
908impl<'a> From<&'a str> for BytesVec {
909    #[inline]
910    fn from(src: &'a str) -> BytesVec {
911        BytesVec::from(src.as_bytes())
912    }
913}