-
Notifications
You must be signed in to change notification settings - Fork 103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Async loading #304
Comments
Whoa, thanks for this, this is a lot of work! Although I haven't quite had a detailed look into your code yet, I like the approach of making internal constructors async while leaving the 'external' interface of |
Thanks for the nice comments :) The readme mentions some gotchas with WASM because the crate currently doesn't support async loading. I don't know much about WASM, but I suppose it would work on WASM targets now? Do we need additional work/documentation for these targets? |
I'm not completely sure, we could try compiling a simple example to wasm and see if that works. If any issues arise then we can fix those or write about how to fix them if it's some user-side thing |
That's some great work! I'm wondering whether you'd be willing to do a separate pull request for just the switch to |
Sorry for the late response ! I don't have much time to work on this at the moment, but I'll try this when I do. |
@Debaug That's alright, finding time is always the issue but at least we're not generally in a rush! You can also consider just opening a draft PR with your changes for now. Maybe somebody else can help either with finishing it or separating it into smaller changes. |
Working with async I/O is currently tricky as
ResourceReader
is not capable of providing a reader asynchronously. I've needed async loading in a project before, so I think adding it as a functionality would be a worthwhile change.I've implemented a solution here. Broadly speaking, I made the following changes:
Switched from
xml-rs
toquick-xml
, which provides async reading. This was also proposed in Consider switching from xml-rs to quick-xml #137. A noticeable difference between these crates is thatxml-rs
expects aRead
whilequick-xml
expects aBufRead
. Accordingly, I've changed theRead
bound onResourceReader::Resource
toBufRead
. This is technically a breaking change, but is worth it in my opinion: most people probably only useFilesystemResourceReader
orstd::io::Cursor
, which would both still work. Otherwise, wrapping a reader instd::io::BufReader
should not be a difficult change (and of course, we're before 1.0).Added an
AsyncResourceReader
trait, mirroringResourceReader
, that asynchronously provides atokio::io::AsyncBufRead
.Removed the
ResourceReader
bound onLoader
and moved it to the loading functions to which I added async counterparts. The alternative would be to create a separateAsyncLoader
struct: this may have the advantage of better communicating intent by placing bounds on the loader types themselves.As for the implementation, I've gone down the somewhat 'hacky' road of taking the lowest common denominator and making the whole parsing process async. To make it compatible with both sync and async readers, I've added two new internal traits:
Reader
, which abstractsquick_xml::Reader
and delegates to eitherquick_xml::Reader::read_event_into
orquick_xml::Reader::read_event_into_async
depending on the implementor, andReadFrom
, which abstractsResourceReader
andAsyncResourceReader
by providing aReader
. On the other end, to get back to the sync world from the async parsing,Loader::load_tmx_map
andLoader::load_tsx_tileset
'execute' the futures withFutureExt::now_or_never
from thefutures
crate.Otherwise, further work on this solution includes:
So it's not quite ready yet.
The text was updated successfully, but these errors were encountered: