I have a function that will read from a std::io::Read and deserialize the data. I'd like to implement a custom AssetLoader, but AssetLoader::load() gives me an already-loaded byte array. I could use a std::io::Cursor to read from that buffer, but that would introduce an unnecessary copy. Is there any way to have my custom loader use a std::io::Read?
#Control read() in custom asset loader?
1 messages · Page 1 of 1 (latest)
i don't think std::io::Cursor will introduce an unnecessary copy. You're talking about a copy of the byte array right?
Yes. The function I have creates a new buffer and reads into it before deserializing.
you can put a &[u8] into a Cursor and it won't copy the data
Oh, perhaps I'm just confused then
it will copy the &[u8] itself, but that's just a pointer under the hood. 8 bytes no matter how much data it's pointing to.
So I'm passing the Cursor to another function that calls read_to_end(&mut buffer). And I'm concerned that a copy occurs there.
this is all cursor actually looks like under the hood:
pub struct Cursor<T> {
inner: T,
pos: u64,
}
it's a very simple struct that just tracks how far along the thing on the inside it's read. If you pass in a &[u8] then it just stores that and never copies any significant amount of data
that will copy all unread data from the buffer inside Cursor to buffer
it may not be an unnecessary copy, but if it is then there's nothing that asset loaders could do to fix that, the function itself would have to be changed to avoid it
Ok. Thanks for the explanation!
remember to add ✅ to the question if it's resolved!
oh hm that's not what I expected
should be next to the "📁 Assets" filter
Hm...
there we go
I had to use the post menu > "edit tags"