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, BufMut, Bytes, BytesMut, buf::IntoIter, buf::UninitSlice, debug};
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) storage: 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            storage: 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            storage: 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            storage: 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.storage.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.storage.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(), 64);
167    /// ```
168    #[inline]
169    pub fn capacity(&self) -> usize {
170        self.storage.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            storage: self.storage.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!(1013, 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                storage: self.storage.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.storage.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.storage.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.storage.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(), 64);
426    ///
427    /// drop(other);
428    /// buf.reserve(128);
429    ///
430    /// assert_eq!(buf.capacity(), 128);
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.storage.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.storage.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.storage.as_ref()
502    }
503
504    #[inline]
505    fn advance(&mut self, cnt: usize) {
506        assert!(
507            cnt <= self.storage.len(),
508            "cannot advance past `remaining` len:{} delta:{}",
509            self.storage.len(),
510            cnt
511        );
512        unsafe {
513            self.storage.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.storage.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.storage.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.storage.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.storage.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 = self.storage.as_ptr();
599            bytes::buf::UninitSlice::from_raw_parts_mut(ptr.add(len), self.capacity() - len)
600        }
601    }
602
603    #[inline]
604    fn put_slice(&mut self, src: &[u8]) {
605        BufMut::put_slice(self, src)
606    }
607
608    #[inline]
609    fn put_u8(&mut self, n: u8) {
610        BufMut::put_u8(self, n)
611    }
612
613    #[inline]
614    fn put_i8(&mut self, n: i8) {
615        BufMut::put_i8(self, n)
616    }
617}
618
619impl AsRef<[u8]> for BytesVec {
620    #[inline]
621    fn as_ref(&self) -> &[u8] {
622        self.storage.as_ref()
623    }
624}
625
626impl AsMut<[u8]> for BytesVec {
627    #[inline]
628    fn as_mut(&mut self) -> &mut [u8] {
629        self.storage.as_mut()
630    }
631}
632
633impl Deref for BytesVec {
634    type Target = [u8];
635
636    #[inline]
637    fn deref(&self) -> &[u8] {
638        self.as_ref()
639    }
640}
641
642impl DerefMut for BytesVec {
643    #[inline]
644    fn deref_mut(&mut self) -> &mut [u8] {
645        self.storage.as_mut()
646    }
647}
648
649impl Eq for BytesVec {}
650
651impl PartialEq for BytesVec {
652    #[inline]
653    fn eq(&self, other: &BytesVec) -> bool {
654        self.storage.as_ref() == other.storage.as_ref()
655    }
656}
657
658impl Default for BytesVec {
659    #[inline]
660    fn default() -> BytesVec {
661        BytesVec::new()
662    }
663}
664
665impl Borrow<[u8]> for BytesVec {
666    #[inline]
667    fn borrow(&self) -> &[u8] {
668        self.as_ref()
669    }
670}
671
672impl BorrowMut<[u8]> for BytesVec {
673    #[inline]
674    fn borrow_mut(&mut self) -> &mut [u8] {
675        self.as_mut()
676    }
677}
678
679impl PartialEq<Bytes> for BytesVec {
680    fn eq(&self, other: &Bytes) -> bool {
681        other[..] == self[..]
682    }
683}
684
685impl PartialEq<BytesMut> for BytesVec {
686    fn eq(&self, other: &BytesMut) -> bool {
687        other[..] == self[..]
688    }
689}
690
691impl fmt::Debug for BytesVec {
692    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
693        fmt::Debug::fmt(&debug::BsDebug(self.storage.as_ref()), fmt)
694    }
695}
696
697impl fmt::Write for BytesVec {
698    #[inline]
699    fn write_str(&mut self, s: &str) -> fmt::Result {
700        if self.remaining_mut() >= s.len() {
701            self.put_slice(s.as_bytes());
702            Ok(())
703        } else {
704            Err(fmt::Error)
705        }
706    }
707
708    #[inline]
709    fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> fmt::Result {
710        fmt::write(self, args)
711    }
712}
713
714impl IntoIterator for BytesVec {
715    type Item = u8;
716    type IntoIter = IntoIter<BytesVec>;
717
718    fn into_iter(self) -> Self::IntoIter {
719        IntoIter::new(self)
720    }
721}
722
723impl<'a> IntoIterator for &'a BytesVec {
724    type Item = &'a u8;
725    type IntoIter = std::slice::Iter<'a, u8>;
726
727    fn into_iter(self) -> Self::IntoIter {
728        self.as_ref().iter()
729    }
730}
731
732impl FromIterator<u8> for BytesVec {
733    fn from_iter<T: IntoIterator<Item = u8>>(into_iter: T) -> Self {
734        let iter = into_iter.into_iter();
735        let (min, maybe_max) = iter.size_hint();
736
737        let mut out = BytesVec::with_capacity(maybe_max.unwrap_or(min));
738        for i in iter {
739            out.reserve(1);
740            out.put_u8(i);
741        }
742
743        out
744    }
745}
746
747impl<'a> FromIterator<&'a u8> for BytesVec {
748    fn from_iter<T: IntoIterator<Item = &'a u8>>(into_iter: T) -> Self {
749        into_iter.into_iter().copied().collect::<BytesVec>()
750    }
751}
752
753impl Extend<u8> for BytesVec {
754    fn extend<T>(&mut self, iter: T)
755    where
756        T: IntoIterator<Item = u8>,
757    {
758        let iter = iter.into_iter();
759
760        let (lower, _) = iter.size_hint();
761        self.reserve(lower);
762
763        for b in iter {
764            self.put_u8(b);
765        }
766    }
767}
768
769impl<'a> Extend<&'a u8> for BytesVec {
770    fn extend<T>(&mut self, iter: T)
771    where
772        T: IntoIterator<Item = &'a u8>,
773    {
774        self.extend(iter.into_iter().copied())
775    }
776}
777
778impl PartialEq<[u8]> for BytesVec {
779    fn eq(&self, other: &[u8]) -> bool {
780        &**self == other
781    }
782}
783
784impl<const N: usize> PartialEq<[u8; N]> for BytesVec {
785    fn eq(&self, other: &[u8; N]) -> bool {
786        &**self == other
787    }
788}
789
790impl PartialEq<BytesVec> for [u8] {
791    fn eq(&self, other: &BytesVec) -> bool {
792        *other == *self
793    }
794}
795
796impl<const N: usize> PartialEq<BytesVec> for [u8; N] {
797    fn eq(&self, other: &BytesVec) -> bool {
798        *other == *self
799    }
800}
801
802impl<const N: usize> PartialEq<BytesVec> for &[u8; N] {
803    fn eq(&self, other: &BytesVec) -> bool {
804        *other == *self
805    }
806}
807
808impl PartialEq<str> for BytesVec {
809    fn eq(&self, other: &str) -> bool {
810        &**self == other.as_bytes()
811    }
812}
813
814impl PartialEq<BytesVec> for str {
815    fn eq(&self, other: &BytesVec) -> bool {
816        *other == *self
817    }
818}
819
820impl PartialEq<Vec<u8>> for BytesVec {
821    fn eq(&self, other: &Vec<u8>) -> bool {
822        *self == other[..]
823    }
824}
825
826impl PartialEq<BytesVec> for Vec<u8> {
827    fn eq(&self, other: &BytesVec) -> bool {
828        *other == *self
829    }
830}
831
832impl PartialEq<String> for BytesVec {
833    fn eq(&self, other: &String) -> bool {
834        *self == other[..]
835    }
836}
837
838impl PartialEq<BytesVec> for String {
839    fn eq(&self, other: &BytesVec) -> bool {
840        *other == *self
841    }
842}
843
844impl<'a, T: ?Sized> PartialEq<&'a T> for BytesVec
845where
846    BytesVec: PartialEq<T>,
847{
848    fn eq(&self, other: &&'a T) -> bool {
849        *self == **other
850    }
851}
852
853impl PartialEq<BytesVec> for &[u8] {
854    fn eq(&self, other: &BytesVec) -> bool {
855        *other == *self
856    }
857}
858
859impl PartialEq<BytesVec> for &str {
860    fn eq(&self, other: &BytesVec) -> bool {
861        *other == *self
862    }
863}
864
865impl PartialEq<BytesVec> for Bytes {
866    fn eq(&self, other: &BytesVec) -> bool {
867        other[..] == self[..]
868    }
869}
870
871impl PartialEq<BytesVec> for BytesMut {
872    fn eq(&self, other: &BytesVec) -> bool {
873        other[..] == self[..]
874    }
875}
876
877impl From<BytesVec> for Bytes {
878    #[inline]
879    fn from(b: BytesVec) -> Self {
880        b.freeze()
881    }
882}
883
884impl<'a> From<&'a [u8]> for BytesVec {
885    #[inline]
886    fn from(src: &'a [u8]) -> BytesVec {
887        BytesVec::copy_from_slice(src)
888    }
889}
890
891impl<const N: usize> From<[u8; N]> for BytesVec {
892    #[inline]
893    fn from(src: [u8; N]) -> BytesVec {
894        BytesVec::copy_from_slice(src)
895    }
896}
897
898impl<'a, const N: usize> From<&'a [u8; N]> for BytesVec {
899    #[inline]
900    fn from(src: &'a [u8; N]) -> BytesVec {
901        BytesVec::copy_from_slice(src)
902    }
903}
904
905impl<'a> From<&'a str> for BytesVec {
906    #[inline]
907    fn from(src: &'a str) -> BytesVec {
908        BytesVec::from(src.as_bytes())
909    }
910}
911
912impl From<BytesVec> for BytesMut {
913    #[inline]
914    fn from(src: BytesVec) -> BytesMut {
915        BytesMut {
916            storage: src.storage.into_storage(),
917        }
918    }
919}