Skip to content
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

Add API to directly load a file #19

Merged
merged 1 commit into from
Aug 10, 2024

Conversation

psychon
Copy link
Contributor

@psychon psychon commented Aug 10, 2024

So far, the parse_xcursor() expected a byte slice as its input. Since the use of this crate is usually in reading cursor files, that means one has to first load the whole file to memory and then give the result to this crate, which copies some of that data.

For example, here is wayland-cursor using read_to_end() to load a file into memory and then passing the result to xcursor-rs:

https://github.com/Smithay/wayland-rs/blob/8fa95cfaae493f748d994ae2aee5433dd183e4ea/wayland-cursor/src/lib.rs#L250-L251

In this commit I want to change this. For API stability reasons, I am not touching parse_xcursor. Instead, a new function parse_xcursor_stream() is added. This function can be called with anything that implements both std::io::Read and Seek. The result is either a list of images or a std::io::Error. The existing parse_xcursor() simply forwards to this new function using std::io::Cursor.

All the parsing functions are changed to take a &mut impl Read. The internal StreamExt trait is changed to work on Read. Luckily, the use of Seek is so far contained in parse_xcursor_stream().

Since the new parse_xcursor_stream() function returns an std::io::Error, tag mismatches need to be turned into such an Error. For that, I simply used ErrorKind::Other with the text "Tag mismatch". This error is externally visible through this new function.

No behavioural changes to the code are intended.


Feel free to suggest another / a better file name for this new function, if you have some preference. I just went with the first thing that came to my mind.

Sadly, this PR conflicts with my other PR. If you like both PRs, I would prefer if this one is merged first, since it is the more complicated one. I can then update the other PR.

So far, the parse_xcursor() expected a byte slice as its input. Since
the use of this crate is usually in reading cursor files, that means one
has to first load the whole file to memory and then give the result to
this crate, which copies some of that data.

For example, here is wayland-cursor using read_to_end() to load a file
into memory and then passing the result to xcursor-rs:

https://github.com/Smithay/wayland-rs/blob/8fa95cfaae493f748d994ae2aee5433dd183e4ea/wayland-cursor/src/lib.rs#L250-L251

In this commit I want to change this. For API stability reasons, I am
not touching parse_xcursor. Instead, a new function
parse_xcursor_stream() is added. This function can be called with
anything that implements both std::io::Read and Seek. The result is
either a list of images or a std::io::Error. The existing
parse_xcursor() simply forwards to this new function using
std::io::Cursor.

All the parsing functions are changed to take a &mut impl Read.  The
internal StreamExt trait is changed to work on Read. Luckily, the use of
Seek is so far contained in parse_xcursor_stream().

Since the new parse_xcursor_stream() function returns an std::io::Error,
tag mismatches need to be turned into such an Error. For that, I simply
used ErrorKind::Other with the text "Tag mismatch". This error is
externally visible through this new function.

No behavioural changes to the code are intended.

Signed-off-by: Uli Schlachter <psychon@znc.in>
@esposm03
Copy link
Owner

LGTM, thanks for the contribution!

@esposm03 esposm03 merged commit eff2da4 into esposm03:master Aug 10, 2024
1 check passed
@psychon psychon deleted the direct-file-reading branch August 10, 2024 10:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants