#Control read() in custom asset loader?

1 messages · Page 1 of 1 (latest)

leaden oak
#

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?

limber gale
#

i don't think std::io::Cursor will introduce an unnecessary copy. You're talking about a copy of the byte array right?

leaden oak
#

Yes. The function I have creates a new buffer and reads into it before deserializing.

limber gale
#

you can put a &[u8] into a Cursor and it won't copy the data

leaden oak
#

Oh, perhaps I'm just confused then

limber gale
#

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.

leaden oak
#

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.

limber gale
#

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

limber gale
#

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

leaden oak
#

Ok. Thanks for the explanation!

limber gale
#

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

leaden oak
#

Hm...

limber gale
#

there we go

leaden oak
#

I had to use the post menu > "edit tags"