@@ -170,6 +170,55 @@ impl Bytes {
170170 }
171171 }
172172
173+ /// Constructs a new `Bytes` that takes the ownership of the `slice`.
174+ ///
175+ /// # Additional Information
176+ ///
177+ /// `slice` **must point to a heap-allocated memory**.
178+ /// `slice`, or its owner, like, e.g., `Vec`, **must not be used
179+ /// after the ownership is transferred to the newly created `Bytes`**.
180+ ///
181+ /// Violating the above restrictions results in an undefined behavior.
182+ ///
183+ /// To create a new `Bytes` from a `raw_slice` that copies the slice content
184+ /// and does not take the ownership, use `Bytes::from(raw_slice)`.
185+ ///
186+ /// # Arguments
187+ ///
188+ /// * `slice`: [raw_slice] - The heap-allocated slice whose ownership is transferred to the `Bytes`.
189+ ///
190+ /// # Returns
191+ ///
192+ /// * [Bytes] - A new `Bytes` whose content is the original content of the `slice`.
193+ ///
194+ /// # Examples
195+ ///
196+ /// ```sway
197+ /// use std::bytes::Bytes;
198+ /// use std::vec::Vec;
199+ ///
200+ /// fn foo() {
201+ /// let mut source = Vec::<u8>::new();
202+ /// source.push(1u8);
203+ ///
204+ /// let bytes = Bytes::from_moved_raw_slice(source.as_raw_slice());
205+ ///
206+ /// // ** `source` must not be used after this point. **
207+ ///
208+ /// assert_eq(bytes.get(0).unwrap(), 1u8);
209+ /// }
210+ /// ```
211+ pub fn from_moved_raw_slice (slice : raw_slice ) -> Self {
212+ let len_and_capacity = slice . number_of_bytes ();
213+ Self {
214+ buf : RawBytes {
215+ ptr : slice . ptr (),
216+ cap : len_and_capacity ,
217+ },
218+ len : len_and_capacity ,
219+ }
220+ }
221+
173222 /// Appends an element to the back of a `Bytes` collection.
174223 ///
175224 /// # Arguments
@@ -541,6 +590,9 @@ impl Bytes {
541590
542591 /// Clears the `Bytes`, removing all values.
543592 ///
593+ /// Note that this method has no effect on the allocated capacity
594+ /// of the `Bytes`.
595+ ///
544596 /// # Examples
545597 ///
546598 /// ```sway
@@ -554,7 +606,6 @@ impl Bytes {
554606 /// }
555607 /// ```
556608 pub fn clear (ref mut self ) {
557- self . buf = RawBytes :: new ();
558609 self . len = 0 ;
559610 }
560611
@@ -752,11 +803,12 @@ impl Bytes {
752803
753804 // reallocate with combined capacity, write `slice`, set buffer capacity
754805 if self . buf. cap < both_len {
755- let new_slice = raw_slice :: from_parts :: <u8 >(
756- realloc_bytes (self . buf. ptr, self . buf. cap, both_len ),
757- both_len ,
758- );
759- self . buf = RawBytes :: from (new_slice );
806+ // `realloc_bytes` already returns a fresh buffer that owns the
807+ // existing content, so we take its ownership directly into `RawBytes`
808+ self . buf = RawBytes {
809+ ptr : realloc_bytes (self . buf. ptr, self . buf. cap, both_len ),
810+ cap : both_len ,
811+ };
760812 }
761813
762814 let new_ptr = self . buf. ptr. add_uint_offset (other_start );
@@ -1104,7 +1156,15 @@ impl TryInto<b256> for Bytes {
11041156impl From <raw_slice > for Bytes {
11051157 /// Creates a `Bytes` from a `raw_slice`.
11061158 ///
1107- /// ### Examples
1159+ /// # Additional Information
1160+ ///
1161+ /// The content of the `slice` gets copied to a newly created `Bytes`
1162+ /// that allocates its own buffer.
1163+ ///
1164+ /// To take the ownership of the `slice` and move it to the newly
1165+ /// created `Bytes` without copying the content, use `Bytes::from_moved_raw_slice`.
1166+ ///
1167+ /// # Examples
11081168 ///
11091169 /// ```sway
11101170 /// use std:bytes::Bytes;
@@ -1121,10 +1181,10 @@ impl From<raw_slice> for Bytes {
11211181 /// let vec_as_raw_slice = vec.as_raw_slice();
11221182 /// let bytes = Bytes::from(vec_as_raw_slice);
11231183 ///
1124- /// assert (bytes.len == 3);
1125- /// assert (bytes.get(0).unwrap() == a);
1126- /// assert (bytes.get(1).unwrap() == b);
1127- /// assert (bytes.get(2).unwrap() == c);
1184+ /// assert_eq (bytes.len, 3);
1185+ /// assert_eq (bytes.get(0).unwrap(), a);
1186+ /// assert_eq (bytes.get(1).unwrap(), b);
1187+ /// assert_eq (bytes.get(2).unwrap(), c);
11281188 /// ```
11291189 fn from (slice : raw_slice ) -> Self {
11301190 Self {
0 commit comments