Is this worth a crate? (Arc<[T]> with subdivision)

In working on my latest project I've found this type (with T = u8) very useful for parsing a binary format:

struct ArcSlice<T> {
    underlying: Arc<[T]>,
    start: usize,
    end: usize,
}

Here's a playground with some key trait implementations and an example useful method. The idea is that ArcSlice { underlying, start, end } acts like &underlying[start..end], only the Arc gives you the extra ability to slice up the underlying memory without thinking about ownership or performing additional allocations. It's very similar to, and significantly inspired by, the bytes::Bytes type, but it's significantly simpler and unsafe-free because it sticks to a single "backend" for the underlying field, whereas Bytes allows you to use e.g. &'static [u8] in place of Arc<[u8]>.

I'm wondering whether those differences from Bytes justify publishing this as a tiny crate. Is this something other people might conceivably find useful?

owning_ref::OwningRef is also another way to express this. It's also more efficient and flexible (you can use any function available on [T], even third party ones) however it isn't unsafe-free

Doesn't the Deref<Target = [T]> impl (in the playground) mean that ArcSlice has this property as well?

What I mean was that you can map that slice to what you want, you're not limited to just that split_off_before or other hardcoded methods you need to provide in the library.

1 Like

I went ahead and published it:

https://crates.io/crates/rc_slice

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.