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