#collections #buffers

range-split

Utilities for splitting sequences with range parameters

5 releases

✓ Uses Rust 2018 edition

new 0.2.0 Dec 1, 2019
0.2.0-beta.2 Nov 28, 2019
0.2.0-beta.1 Nov 27, 2019
0.1.0 Oct 24, 2019
0.1.0-beta.1 Jun 16, 2019

#202 in Rust patterns

47 downloads per month
Used in 1 crate

MIT license

14KB
257 lines

Splitting Sequences with Range Syntax

This library crate provides a trait and support utilities adding convenience methods for splitting sequences accordingly to a given range.

The primary use case for the TakeRange trait are data container libraries such as bytes, where representations of data buffers can be efficiently split into smaller parts, but the inherent methods for doing this tend to be not very mnemonic. Implementations of the trait parameterized with different range types provide convenient polymorphism with the range syntax.

License

This project is licensed under the MIT license.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in range-split by you, shall be licensed as MIT, without any additional terms or conditions.


lib.rs:

Utilities for splitting sequences with range parameters.

The TakeRange trait provides polymorphic, easily memorizable methods for splitting indexed sequences with a parameter given in range syntax.

# #[cfg(features = "bytes")]
# fn main() {
# use bytes::Bytes;
use range_split::TakeRange;

let mut buf = Bytes::from("Hello, world");
let p = buf.take_range(..5);
buf.remove_range(2..);
assert_eq!(p, "Hello");
assert_eq!(buf, ", ");
# }
# #[cfg(not(features = "bytes"))]
# fn main() {}

is equivalent to

# #[cfg(features = "bytes")]
# fn main() {
# use bytes::Bytes;
#
let mut buf = Bytes::from("Hello, world");
let p = buf.split_to(5);
buf.truncate(2);
# }
# #[cfg(not(features = "bytes"))]
# fn main() {}

Implementations of TakeRange are provided for Bytes and BytesMut from the crate bytes if the bytes compile-time feature is enabled.

Dependencies

~155KB