Commit Graph
162 Commits
Author SHA1 Message Date
Scott Lamb 141cbc22cb Rename Vtable::to_* -> Vtable::into_* (#776) 2025-03-07 13:58:07 +00:00
venomousmoog 36675436cc Fix memory leak in owned_to_vec (#773) 2025-03-05 10:44:40 +01:00
Adam Cimarosti 30ee8e9cba Add Bytes::from_owner (#742) 2024-10-29 13:18:03 +01:00
Alice Ryhl 0ac54ca706 Guarantee address in split_off/split_to for empty slices (#740)
Signed-off-by: Alice Ryhl <[email protected]>
2024-10-18 19:46:49 +02:00
Taiki Endo ac46ebdd46 ci: update nightly to nightly-2024-09-15 (#734) 2024-09-17 11:02:45 -04:00
Paolo Barbolini 291df5acc9 Fix double spaces in comments and doc comments (#731) 2024-08-19 10:19:35 +02:00
Anthony Ramine fa1daac3ae Change Bytes::make_mut to impl From<Bytes> for BytesMut (closes #709) (#710)
<Arc<T>>::make_mut returns a &mut T, such an API is doable for Bytes too
and thus we should reserve Bytes::make_mut for that.

Furthermore, it would be helpful to use From<Bytes> as a trait bound
in some cases with other traits such as Hyper's body trait, where Hyper
gives you Bytes values.

Finally, making it impl From<Bytes> for BytesMut means the API is more
easily discoverable as it appears on both Bytes and BytesMut.
2024-05-28 10:14:02 +02:00
Brad Dunbar 4950c50376 Offset from (#705) 2024-05-11 19:41:50 +02:00
Émile Fugulin 86694b0564 Add zero-copy make_mut (#695) 2024-05-05 17:58:00 +02:00
Brad Dunbar ce09d7d358 Bytes::split_off - check fast path first (#693)
Follow up to https://github.com/tokio-rs/bytes/pull/689

* If `at == self.len()`, we already know `at <= self.len()`.
* If `at == 0`, we already know `at <= self.len()`.
2024-04-24 08:23:39 -04:00
tison b5fbfc3edb perf: improve Bytes::copy_to_bytes (#688)
Signed-off-by: tison <[email protected]>
2024-04-10 16:45:31 +02:00
Brad Dunbar 4eb62b912a Bytes::split_to - check fast path first (#689)
If `at == self.len()` then we already know `at <= self.len()`. If
`at == 0`, it can't be greater than `self.len()`.
2024-04-10 10:09:09 +02:00
Brad Dunbar 0d4cc7ffed Bytes: Use ManuallyDrop instead of mem::forget (#678) 2024-04-08 17:05:04 +02:00
Brad Dunbar d2e7abdb29 refactor: make parameter mut in From<Vec> (#667)
Instead of re-declaring `vec`, we can just use a mut parameter.
2024-01-31 09:41:23 -05:00
Brad Dunbar 0ba3b4c4cd Remove unnecessary namespace qualifier (#660) 2024-01-28 14:07:11 +03:30
Cyborus04 0864aea970 add Bytes::is_unique (#643) 2024-01-19 23:59:30 +01:00
Gabriel Goller 72cbb92e0e docs: fix broken links (#639)
Fixed a few broken links and converted a lot of them from the
html-link to intra-doc links.
2023-11-16 06:24:21 -05:00
DanielBandDaniel Bauman bde8c50703 docs: typo fix (#637)
Co-authored-by: Daniel Bauman <[email protected]>
2023-10-19 21:50:24 +02:00
Alice Ryhl fd9243f9e2 Various cleanup (#635) 2023-10-02 15:40:02 +02:00
brian m. carlson 05e9d5cab9 Avoid large reallocations when freezing BytesMut (#592)
When we freeze a BytesMut, we turn it into a Vec, and then convert that
to a Bytes.  Currently, this happen using Vec::into_boxed_slice, which
reallocates to a slice of the same length as the Vev if the length and
the capacity are not equal.  This can pose a performance problem if the
Vec is large or if this happens many times in a loop.

Instead, let's compare the length and capacity, and if they're the same,
continue to handle this using into_boxed_slice.  Otherwise, since we
have a type of vtable which can handle a separate capacity, the shared
vtable, let's turn our Vec into that kind of Bytes.  While this does not
avoid allocation altogether, it performs a fixed size allocation and
avoids any need to memcpy.
2023-01-31 20:04:22 +01:00
Nicolae MihalacheandNicolae Mihalache c93a94b974 Fix duplicate "the the" typos (#585)
Co-authored-by: Nicolae Mihalache <[email protected]>
2022-12-20 11:49:55 +01:00
Adam Chalmers a36f661354 docs: Bytes::new etc should return Self not Bytes (#568) 2022-08-24 13:30:21 +02:00
Jiahao XUandFrank Steffahn 7553a67be2 Fix amortized asymptotics of BytesMut (#555)
Signed-off-by: Jiahao XU <[email protected]>
Co-authored-by: Frank Steffahn <[email protected]>
2022-07-19 13:17:53 +02:00
Jiahao XUandAlice Ryhl cd188cbd67 Add conversion from Bytes to Vec<u8> (#547)
Signed-off-by: Jiahao XU <[email protected]>
Co-authored-by: Alice Ryhl <[email protected]>
2022-07-13 09:04:23 +02:00
Alice Ryhl 716a0b189e Only avoid pointer casts when using miri (#545) 2022-04-29 22:01:26 +02:00
Alice Ryhl b4b2c18c27 Revert accidental push directly to master
This reverts commit 89061c3238.

Why am I even able to push to master?
2022-04-29 19:49:28 +02:00
Alice Ryhl 89061c3238 Only avoid pointer casts when using miri 2022-04-29 19:47:34 +02:00
Alice Ryhl 8198f9e28e Make strict provenance compatible (#542) 2022-04-16 00:26:58 +02:00
Alice Ryhl 547a32033e Add TSAN support (#541) 2022-04-15 22:46:40 +02:00
Ben Kimock 724476982b Fix aliasing in Clone by using a raw pointer (#523)
Previously, this code produced a &mut[u8] and a Box<[u8]> to the shared
allocation upon cloning it. If the underlying allocation were actually
shared, such as through a &[u8] from the Deref impl, creating either of
these types incorrectly asserted uniqueness of the allocation.

This fixes the example in #522, but Miri still does not pass on this
test suite with -Zmiri-tag-raw-pointers because Miri does not currently
understand int to pointer casts.
2022-04-06 16:59:20 +02:00
Anthony Deschamps e4c723697d docs: redraw layout diagram with box drawing characters. (#539)
I find this diagram very helpful, but a little hard to distinguish
between the boxes and the lines that connect them. This commit redraws
the boxes with line drawing characters so that the boxes appear a
little more solid, and stand out from the other lines.
2022-03-25 10:55:13 +01:00
Rob Ede 0e3b2466f1 Address various clippy warnings (#528) 2022-01-24 09:58:05 +01:00
Cyborus04 d946ef2e91 const-ify Bytes::len and Bytes::is_empty (#514) 2021-11-09 11:41:40 +01:00
Ian Jackson 0e9fa0b602 impl From<Box<[u8]>> for Bytes (#504) 2021-08-24 12:42:22 +02:00
Ralf Jung df20a68356 use Box::into_raw instead of mem-forget-in-disguise (#458) 2020-12-31 15:07:28 +01:00
Alice Ryhl ed71a7beb3 Fix deprecation warning (#457) 2020-12-29 22:46:39 +01:00
Carl Lerche 06907f3e7b Rename Buf/BufMut, methods to chunk/chunk_mut (#450)
The `bytes()` / `bytes_mut()` name implies the method returns the full
set of bytes represented by `Buf`/`BufMut`. To rectify this, the methods
are renamed to `chunk()` and `chunk_mut()` to reflect the partial nature
of the returned byte slice.

`bytes_vectored()` is renamed `chunks_vectored()`.

Closes #447
2020-12-18 11:04:31 -08:00
Carl Lerche 39de065a1e Add Buf::copy_to_bytes(len) (#439)
This method replaces `Buf::to_bytes()`, providing a method that copies a
subset of the remaining buffer into a `Bytes` value. As this is strictly
more flexible, `to_bytes()` is removed.

Fixes: #129, #398
2020-10-20 11:00:35 -07:00
Carl Lerche 5866839e45 use checked addition with range (#438) 2020-10-20 10:26:38 -07:00
Matthias Einwag 5a11c783ec De-emphasize Arc implementation in Bytes description (#436)
The previous description focussed a lot on the `Arc` based implementation
of `Bytes`. Given the vtable based implemetation, this is however not the
only valid implementation. This changes the description a bit in order
to de-emaphasize the `Arc` part, and to describe that other implementations
are possible.

This should also be necessary if the vtable gets public.
2020-10-19 12:32:20 -07:00
Tomasz Miąsko 5cde647c29 Remove unnecessary synchronization when cloning shared representation (#404) 2020-07-02 18:55:16 -07:00
Taiki Endo 1fbf83816b Format with rustfmt (#389)
* Format with rustfmt

* Add rustfmt check to CI
2020-05-22 13:17:30 +09:00
Sean McArthur 5e93fa4c6b Update to loom v0.3 (#381) 2020-05-22 11:23:22 +09:00
Stepan Koltsov f330ef6c4d Do not panic on Bytes::slice_ref on empty slice (#355)
Use case:

```
let bytes: Bytes = ...
let subbytes = bytes.slice(a..b); // where a == b
let slice = &subbytes[..];
let slice_bytes = bytes.slice_ref(slice);
```

Last line should not panic, because `slice` object is derived from
the original `Bytes` object.

Before this commit it panics, because `Bytes::slice` returns a fresh
`Bytes` object when `begin == end`.
2020-01-23 10:29:42 -08:00
南浦月 3853a1fac4 merge hex & debug into fmt (#357) 2020-01-23 10:08:40 -08:00
Andrew Tunnell-Jones 939a5edf3d Fix reversed arguments in PartialOrd impls (#358) 2020-01-23 10:06:00 -08:00
John-John Tedro 788cb158ce Assert that the alignment of Shared is appropriate (#362) 2020-01-23 10:05:20 -08:00
Stepan Koltsov ab028eb6a8 rebuild_boxed_slice instead of rebuild_boxed_vec (#364)
"Promotable" `Bytes` object is constructed from disassembling a
boxed slice object, not a vec.

Thus we should reassemble data into a boxed slice, not into a vec.

Although, it does not create any problems in practice (`Box<[u8]>`
is allocated exactly the same way as `Vec<u8>`), technically it is
a violation of `Vec::from_raw_parts` spec which says that a pointer
"needs to have been previously allocated via `String`/`Vec<T>`".
2020-01-23 10:04:13 -08:00
Sean McArthur e0eebde993 Fix Bytes::truncate losing the original Vec's capacity (#361) 2020-01-22 16:37:53 -08:00
Stepan Koltsov 729bc7c208 Make Bytes::new const fn (#356) 2020-01-09 14:27:28 -08:00