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

Attribute: "read_fn" / "write_fn" #12

Open
neuronsguy opened this issue Aug 24, 2021 · 0 comments · May be fixed by #71
Open

Attribute: "read_fn" / "write_fn" #12

neuronsguy opened this issue Aug 24, 2021 · 0 comments · May be fixed by #71

Comments

@neuronsguy
Copy link

Hi,

I'm trying to use speedy which is a great fit for my use-case, but running into the issue of being unable to implement Readable/Writable structs in crates that I don't own.

It would be very handy to have an attribute like #[speedy(read_fn = my_reader_fn)] that would allow defining a custom function to turn a given field into bytes.

Rough example API:

#[derive(Readable,Writable)]
struct Mine {
    field1: u64,
    #[speedy(read_fn=read_other_crate_struct)]
    #[speedy(write_fn=write_other_crate_struct)]
    field2: OtherCrateStruct
}

fn write_other_crate_struct<T: ?Sized + Writer<C>,C:Context>(val: &OtherCrateStruct, writer: &mut T) -> Result<(), C::Error> {
    todo!()
}

fn read_other_crate_struct<R: Reader<C>,C:Context>(reader: &mut R) -> Result<OtherCrateStruct, C::Error> {
    todo!()
}

This would remove the need to fork crates to add Readable/Writable implementations to them.

If this change is easy to make I'd be happy to do it, although I'm not familiar with macro / derive code so would need some pointers.

tecc added a commit to tecc/speedy that referenced this issue Jul 23, 2024
The `read_with` and `write_with` attributes have been added.
They override the generated implementation for reading/writing individual fields,
similarly to how Serde's `deserialize_with` and `serialize_with` attributes work.

The `with` attribute has also been added as a utility,
which sets both `read_with` and `write_with` simultaneously.

See the README.md for more specific documentation.
Closes koute#12.
@tecc tecc linked a pull request Jul 23, 2024 that will close this issue
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 a pull request may close this issue.

1 participant