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