-
-
Notifications
You must be signed in to change notification settings - Fork 777
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
i128 and u128 integers missing Deserialize impls #1136
Comments
u128 and i128 are going to be stabilized very soon. However, from what I can tell, adding support to serde will be a breaking change. Is serde 2.0 planned? |
They've been stabilized 🎊, will this be included in the next release? |
We can't add them directly, because that would break all users of older rustc stable versions We could add it under a (stable) feature flag |
So I'm trying to add this behind a feature flag in cgm616/serde, but I come across a super weird error when attempting to compile. Can someone check this out for me? I'm running > cargo build --all-features and getting this output:
I really can't figure out what's going on. If anyone could help me that would be great. I've never tried to use feature flags and I've also never worked on serde (or a project of this size) before, so I'm probably pretty misguided in my changes. |
Adding i128 and u128 is a breaking change, as I said before. You're running into some breakage from the change. |
Breaking as in for consumers of the library? Or breaking as in it requires a redesign of the library's internals? I thought you meant the former, and that's what the feature flag is for. |
To add u128 and i128, I literally searched everywhere i64 and u64 were used in the project and evaluated if the new types needed to be added in some way there. If they did, I put it behind a feature flag and moved on. |
It is breaking for people using the library, but that includes the library itself. In those cases the library implements Deserializer or Serializer, such as |
This is what I've done in the case of impl<'de, 'a, E> Deserializer<'de> for BytesDeserializer<'a, E>
where
E: Error,
{
type Error = E;
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
visitor.visit_bytes(self.value)
}
forward_to_deserialize_any! {
bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes
byte_buf option unit unit_struct newtype_struct seq tuple tuple_struct
map struct enum identifier ignored_any
}
#[cfg(feature = "128")]
forward_to_deserialize_any! {
i128 u128
}
} At first I did it all without the feature flag, and it all compiled and tests passed (including new tests I added for i128 and u128). Now I'm heading back through and changing it all to use the flag, but I can't get past this weird error. |
You could try using |
Okay, hmmm. When using that in each individual crate, the methods are present. In addition, those crates compile fine. It's only when compiling at the workspace level that it doesn't seem to work. Is it possible that there's a problem with the feature passing on? |
Oh yeah, that'll be a problem... As far as I know, there's no good way to enable a dependency's feature conditional on a feature of the root crate: rust-lang/cargo#2524 There may be a crazy way to resolve this with additional crates, IDK. |
So in effect there's no way to add i128 and u128 support until serde increases its minimum supported rustc version to the first version that includes these types on stable, which hasn't even come out yet. That's a bit disappointing... I'm going to keep seeing what I can do. |
I think using separate |
Oh, that's a good idea. I'll try that and see what I can come up with. It'll require a lot of duplicated code... but let's see. |
Does it break consumer code if I add the following impls? impl Serialize for u128 {}
impl Serialize for i128 {}
impl Deserialize for u128 {}
impl Deserialize for i128 {} I'm guessing it does... but what if those are all gated? |
I'm guessing those impls aren't possible if we're using an extension to the |
So the problem here is that there's no way to really write the serialize impls with distinct traits like that. (Or I simply don't know how.) For example, say we have the following code: impl BreakingSerializer: Serializer {
fn serialize_i128(
self,
v: i128,
) -> Result<<Self as Serializer>::Ok, <Self as Serializer>::Error>;
}
impl Serialize for i128 {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: BreakingSerializer,
{
serializer.serialize_i128(*self)
}
} Compilation fails with the following error:
So to fix this, we need to add a I'm at a loss how to proceed here. Am I missing something? |
@cgm616 Did you make any further progress on this? Does anyone know of a workaround in the meantime? |
@dtolnay Since |
As a follow up to that last comment, the docs state:
Which is no longer true as of Rust 1.26, thus implying this be a bug (and one I'd love to see squashed). |
This is a bug, but whatever we say about its gravity doesn’t change the fact that (as far as I can tell) fixing it requires a breaking change. And publishing a new serde 2.x version that’s incompatible with 1.x would have a large impact on the ecosystem, particularly if there isn’t an easy way for libraries to support both for their public types. |
I haven't read the whole discussion in this issue but this shouldn't be a breaking change beyond bumping the minimum supported rustc version from 1.13 to 1.26. Rather than exposing a Cargo cfg I would prefer to automatically detect when we are on a sufficiently new compiler and provide the impls without a cfg opt-in. |
That would require some build script trickery, but should be doable. |
If the |
@SimonSapin we would not make it a required method, it would have a default impl that always returns error. |
Based on the discussion in rust-lang/rust#35118 it seems like we are going to need a whitelist of platforms that have reliable support for i128. Right? |
I think the use of an internal config with a build script to enable it based on compiler/platform support is a good idea. I worry about future complications (docs only including certain methods depending on the compiler used to build them, a fracturing of the packages that can work together based on compiler version, stuff like that), but it seems like the best path forward. |
Oh, but again, I'm not sure if this is even possible. See my earlier comment (and errors):
And then the response by @PlasmaPower :
There's no current way to enable the features of a dependency depending on the features of the current crate, making it difficult to gate features for workspace crates. A separate build script per crate in the workspace might work... do build scripts run when building from workspace root? |
Seems to work! Conditionally defining a macro is a good idea, I never thought to do that. I'm excited for this to land. |
I published 1.0.60 with this feature. |
They should behind the
unstable
flag.The text was updated successfully, but these errors were encountered: