Commit Graph

125 Commits

Author SHA1 Message Date
Carl Lerche d58e99485c 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 143861dec0 use checked addition with range (#438) 2020-10-20 10:26:38 -07:00
Matthias Einwag 79842672f4 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 3d28557ac0 Remove unnecessary synchronization when cloning shared representation (#404) 2020-07-02 18:55:16 -07:00
Taiki Endo 9a2e275ae3 Format with rustfmt (#389)
* Format with rustfmt

* Add rustfmt check to CI
2020-05-22 13:17:30 +09:00
Sean McArthur e186368e5b Update to loom v0.3 (#381) 2020-05-22 11:23:22 +09:00
Stepan Koltsov a4b63406c7 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
南浦月 f812a9f70e merge hex & debug into fmt (#357) 2020-01-23 10:08:40 -08:00
Andrew Tunnell-Jones 5369a989a4 Fix reversed arguments in PartialOrd impls (#358) 2020-01-23 10:06:00 -08:00
John-John Tedro 1efd9958a3 Assert that the alignment of Shared is appropriate (#362) 2020-01-23 10:05:20 -08:00
Stepan Koltsov ab3934634c 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 952158eeb2 Fix Bytes::truncate losing the original Vec's capacity (#361) 2020-01-22 16:37:53 -08:00
Stepan Koltsov ec0825fe19 Make Bytes::new const fn (#356) 2020-01-09 14:27:28 -08:00
Sean McArthur 4efc6a7663 Improve assertion messages (#349) 2020-01-07 21:29:36 -08:00
Sean McArthur 419e36e67c Fix Bytes when Vec pointer's LSB is set (#346)
This separates the `SharedVtable` into 3:

- `PromotableEvenVtable`: The original `SharedVtable`, which will
  promote the `Vec` to `Shared` on the first clone, and is selected when
  the `Vec`'s pointer has the LSB unset.
- `PromotableOddVtable`: Similar to the `PromotableEvenVtable`, but
  selected when the `Vec`'s pointer has the LSB set. This vtable differs
  in the masking used when reconstructing the `Vec`.
- `SharedVtable`: This no longer checks if its current kind is `VEC` or
  `ARC`, and is only created by the "promotable" vtables.

This also adds a test using an "odd" global allocator that purposefully
bumps all pointers with alignment of 1.

Closes #343
2019-12-17 13:23:15 -08:00
Sean McArthur 0a99d22ed6 Assert the LSB is 0 when converting Vec into Bytes 2019-12-12 11:46:51 -08:00
Steven Fackler 14222d5a9a Fix conversion of empty vectors to Bytes
Closes #340
2019-12-12 08:33:07 -08:00
Sean McArthur bdd6975e08 Add must_use to split, split_off, and split_to 2019-12-04 12:39:48 -08:00
Mikhail Zabaluev bad4e1b3ab Fix regression in Bytes::truncate (#333)
When the length to truncate is greater than the buffer's current
length, do nothing instead of clearing the contents.
2019-12-01 14:00:42 -08:00
Andrew Tunnell-Jones 777bcd5f31 Make Bytes::from_static a const fn (#311)
Rust versions bumped to the minimum needed for const slice len.
2019-11-20 11:10:03 -08:00
Sean McArthur f840d6d773 Change loom tests to use cfg(loom) internally (#314) 2019-11-13 14:55:25 -08:00
Sean McArthur c3f9cf1d72 Refactor Bytes to use an internal vtable (#298)
Bytes is a useful tool for managing multiple slices into the same region
of memory, and the other things it used to have been removed to reduce
complexity. The exact strategy for managing the multiple references is
no longer hard-coded, but instead backing by a customizable vtable.

- Removed ability to mutate the underlying memory from the `Bytes` type.
- Removed the "inline" (SBO) mechanism in `Bytes`. The reduces a large
  amount of complexity, and improves performance when accessing the
  slice of bytes, since a branch is no longer needed to check if the
  data is inline.
- Removed `Bytes` knowledge of `BytesMut` (`BytesMut` may grow that
  knowledge back at a future point.)
2019-10-16 09:53:36 -07:00
Chris Beck 79359b1526 Add no_std support, by adding an std feature (#281)
To make the library work as `no_std` we add an `std` feature which
is on by default. When it is off, we compile as `no_std` and make
parts of the API that require `std::io` conditional on the `std`
feature.
2019-09-05 14:00:23 -07:00
Taiki Endo cdb1ebff8c Fix one use of MaybeUninit (#291) 2019-09-01 01:33:10 +09:00
Ralf Jung 25214da03f use raw ptr for racy load and add comment (#289) 2019-08-28 14:41:04 -07:00
Douman def58d121a Fix clippy (#285) 2019-08-27 14:19:44 -07:00
Douman e7fe763e95 Make From only for static slices to Bytes 2019-08-27 22:17:26 +02:00
Douman d5a18356a0 Remove IntoBuf/FromBuf (#288)
As consequence Buf::collect is removed as well, which is replaced with `Buf::into_bytes`. The advantage of `Buf::into_bytes` is that it can be optimized in cases where converting a `T: Buf` into a `Bytes` instance is efficient.
2019-08-27 13:09:43 -07:00
nanpuyue d340e88f36 use mem::MaybeUninit instead of mem::uninitialized 2019-08-16 15:49:37 +02:00
Taiki Endo f817432278 Update Bytes to Rust 2018 (#274) 2019-07-26 05:01:22 +09:00
Bruce Mitchener 48770ad686 Fix typos. 2019-07-16 11:18:49 -07:00
Sean McArthur d38e1744fc Use RangeBounds trait for Bytes::slice
- Removes `slice_to` and `slice_from`.
2019-06-10 09:39:27 -07:00
Sean McArthur e3de5089d6 Remove io::Cursor, and implement Buf/BufMut for slices instead (#261) 2019-06-07 12:31:10 -07:00
YetAnotherMinion 5b6fa9cea2 feat: remove impl IntoBuf for Cursor<Self>, impl Buf for Bytes, BytesMut, refactor iterators 2019-06-06 16:59:44 -07:00
Sean McArthur 8413c1cf7a Merge branch 'v0.4.x' into uplift-0.4-commits 2019-06-06 14:08:29 -07:00
Pavel Strakhov ec1e70f44e Panic in BytesMut::split_to when out of bounds (#252) (#253) 2019-04-02 16:24:30 -07:00
Sangguk Lee 334c2fcd07 Use constants in bytes.rs test code (#247) 2019-02-27 10:41:11 -08:00
南浦月 f6ee7a1d09 Impl FromIterator<&'a u8> for BytesMut/Bytes (#244) 2019-01-30 11:05:15 -08:00
Dax Huiberts 0b90945f6b Fix typo in bytes.rs (#243) 2019-01-28 10:06:28 -08:00
Ralf Jung a624a275d4 use raw ptr for potentially racy load (#240) 2018-12-21 11:07:20 -08:00
Ralf Jung be26124797 Be clear about Inner::kind being deliberate UB (#236) 2018-11-25 22:49:35 -08:00
Ralf Jung 87c9f5cb96 Use raw pointers for potentially racy loads (#233)
Shared references assert immutability, so any concurrent access would be UB
disregarding data race concerns.
2018-11-17 07:51:50 -08:00
Federico Mena Quintero ae519ed318 Add a subslice function for Bytes (#198) (#208)
This lets us take Bytes and a &[u8] slice that is contained in it, and
create a new Bytes that corresponds to that subset slice.

Closes #198
2018-09-01 19:57:31 -07:00
Sean McArthur 3e69a8146b inline Bytes::len and Bytes::is_empty (#211) 2018-07-12 20:17:27 -07:00
Roman 95d64d6ab7 Fix cargo doc error on nightly caused by broken link to footnote (#218) 2018-07-12 20:15:53 -07:00
Carl Lerche cd8d69c8f4 Merge branch 'v0.4.x' 2018-07-12 20:11:51 -07:00
Sean McArthur fbb7272cba inline Bytes::len and Bytes::is_empty (#211) 2018-07-12 19:05:35 -07:00
Roman 8180993218 Fix cargo doc error on nightly caused by broken link to footnote (#218) 2018-07-05 10:13:01 -07:00
Sean McArthur 1f03ad7fd3 Optimize Inner::shallow_clone (#217)
- Clones when the kind is INLINE or STATIC are sped up by over double.
- Clones when the kind is ARC are spec up by about 1/3.
2018-07-03 15:21:26 -07:00
Carl Lerche 2b10290b5d Merge branch 'v0.4.x' 2018-05-25 14:15:00 -07:00