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

Implement Serialize/Deserialize for std::io::Error #1940

Closed
Ekleog opened this issue Jan 2, 2021 · 2 comments
Closed

Implement Serialize/Deserialize for std::io::Error #1940

Ekleog opened this issue Jan 2, 2021 · 2 comments

Comments

@Ekleog
Copy link

Ekleog commented Jan 2, 2021

Hello!

I'm doing a rust program with webassembly “plugins” (a SMTP server, the code is available here). While doing so, there are -> std::io::Result<T> functions that I'd like to implement in webassembly, that may end up calling back into rust to perform IO.

However, in order to do that (be it “expose a -> std::io::Result<T> callback to wasm” or “expose a -> std::io::Result<T> callback from wasm”), I need to have a way to pass an std::io::Error to/from wasm, which basically means serialization (I currently use bincode semi-manually, though hopefully someday wasm-bindgen will support this use case directly).

Currently, what I do is pass a Result<T, String> and then rebuild with std::io::ErrorKind::Other, because I'd rather avoid having lines and lines of serialization boilerplate.

However, I'm thinking it may make sense to have serde already have implementations for std::io::Error as a (std::io::ErrorKind, String)? This is not a perfect identity (because it loses the potential for having raw os errors), but I'm thinking it should hopefully be good enough?

Related to #1480, though I think being able to (de)serialize std::io::Result is a lot less use-case-specific than just std::io::ErrorKind (although I'd guess it'd also solve that other use case as I suppose the implementation will most likely go through serializing ErrorKind)

Anyway, thank you for this awesome crate :)

@dtolnay
Copy link
Member

dtolnay commented Jan 23, 2022

I'd prefer not to build this into the serde crate. But it would be possible for another crate to provide serialization/deserialization logic for std::io::Result that's usable through serde's serialize_with and deserialize_with attributes.

@dtolnay dtolnay closed this as completed Jan 23, 2022
@Ekleog
Copy link
Author

Ekleog commented Jun 26, 2022

I understand! FWIW, I built https://crates.io/crates/serde-error in the meantime. To the best of my knowledge it is bug-free, feature-complete and not only used by me (though the download count makes me guess it's not highly used). So feel free to link to it should other people request it! :)

Thank you for serde anyway, it's great! :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants