-
-
Notifications
You must be signed in to change notification settings - Fork 792
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
Pass arguments to serialize_with function #1059
Comments
This almost already works if you define impl base64::Config {
fn serialize<S>(&self, bytes: &[u8], serializer: S) -> Result;
}
const CUSTOM_B64: base64::Config = /* ... */;
#[derive(Serialize)]
struct S {
#[serde(serialize_with = "CUSTOM_B64")]
bytes: Vec<u8>,
} Except that the generated code tries to invoke |
With RFC 2000, the solution may just be wait for const generics. I think that would require no code change in Serde. const CUSTOM_B64: base64::Config = /* ... */;
#[derive(Serialize)]
struct S {
#[serde(serialize_with = "B64::<CUSTOM_B64>")]
bytes: Vec<u8>,
}
struct B64<const CONFIG: base64::Config>;
impl<const CONFIG: base64::Config> B64<CONFIG> {
fn serialize<S>(bytes: &[u8], serializer: S) -> Result;
} |
String based collections use Display and FromStr for de/serialization of types. Each value is then separated by a seperator, commonly space or comma. A similar wish was voiced at serde-rs/serde#581 The implementation uses something like serde-rs/serde#1059 to support arbitrary, even library user defined, separators.
4: Miscellanious changes r=jonasbb a=jonasbb * Rename module * Some improvements to the doc comments * Ignore the Cargo.lock file bors r+ 5: String collections r=jonasbb a=jonasbb String based collections use Display and FromStr for de/serialization of types. Each value is then separated by a seperator, commonly space or comma. A similar wish was voiced at serde-rs/serde#581 The implementation uses something like serde-rs/serde#1059 to support arbitrary, even library user defined, separators. bors r+ Co-authored-by: Jonas Bushart <[email protected]>
Another possibly useful use case: Use existing methods with additional parameters. fn serialize_bigint_as_str<S>(val: &BigInt, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer
{
serializer.serialize_str(&val.to_str_radix(10))
} What if you could just pass: #[derive(Serialize)]
struct Foo {
#[serde(serialize_with="BigInt::to_str_radix(10)")]
value: BigInt
} How hard it would be to add an attribute that would work on an object, and would expect a valid expression such as Edit: Or possibly add |
This comment has been minimized.
This comment has been minimized.
I am closing the issue as I don't consider this important to address. The suggested workaround is writing a named function that forwards to a call to your serialization function with the right additional arguments. fn ser_with_custom_base64<S>(bytes: &[u8], serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
const CUSTOM_B64: base64::Config = /* ... */;
ser_base64(bytes, serializer, CUSTOM_B64)
} |
With the example of base64 serialization, there are probably only ever a few variation of arguments you might pass, so adding this feature is admittedly not that critical. More painful though, is something like Date formatting, where there are potentially a lot of different arguments. It'd be nice to be able to do something like this:
Rather than something like:
|
This better handle with #1550, IMHO. |
For anybody reading this: a neat trick that I've found for cases where the configuration data is very simple is to pass the value in as a const generic, like so: #[derive(Serialize)]
struct Foo {
#[serialize_with = "serialize_bar::<_, 5>"]
bar: Bar,
}
fn serialize_bar<S: Serializer, const N: u32>(&Bar, ser: S) -> Result<S::Ok, S::Error> {
...
} |
Thanks for sharing @zesterer. Once (if)
|
Another fix (the most straightforward, simplest, and most powerful, in my opinion) would be to allow expressions as functions, not just identifiers. That would be consistent with Rust's design of function calls: in The original code snippet of this issue would be written as: const CUSTOM_B64: base64::Config = /* ... */;
#[derive(Serialize)]
struct S {
#[serde(serialize_with = "|b, s| ser_base64(b, s, CUSTOM_B64)")]
bytes: Vec<u8>,
}
fn ser_base64<S>(bytes: &[u8], serializer: S, config: base64::Config) -> Result; |
Currently serialize_with always takes 2 arguments, the value being serialized and the serializer, and deserialize_with always takes 1 argument, the deserializer.
In some cases it may be convenient to pass an additional argument. For example formatting to base64 with a custom base64 configuration:
The text was updated successfully, but these errors were encountered: