Closed rklaehn closed 1 year ago
Note: I am not saying that this is the best way forward. Any of the other two ( #526 and #437 ) would also do.
It would just be great if there was any way to create a Bytes for something like a memory mapped buffer without copying.
Closing as duplicate of #437.
I have a situation where I have a memory mapped file and want to pass a slice of it around unencumbered by lifetimes, but without copying. I have looked into the Bytes crate, but could not use it since there is no way to create a custom Bytes instance.
See https://github.com/tokio-rs/bytes/issues/526 and https://github.com/tokio-rs/bytes/issues/437 .
I ended up writing my own bytes like struct that is a byte slice and an
Arc<dyn Any>
. See Blob .This works very similar to Bytes, except that the reference counting is done by the
Arc<dyn Any>
instead of a custom VTable. There is a Constructor where you pass in a slice and anArc<dyn Any>
and promise that the slice will be valid as long as whatever theArc<dyn Any>
points to is kept alive.You could easily implement a custom VTable for this approach and expose a similar method in the Bytes crate. I think it is a pretty minimal way to allow for custom Bytes instances without having to expose internals like the VTable.
I guess you would have to do the same for Rc for completeness sake, but still it is much less surface area than approaches that require to make the
VTable
public, and also quite convenient for the user.Here is how it is used: