Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

ContentDeserializer does not support 128-bit integers #2576

Open
thill opened this issue Aug 18, 2023 · 2 comments · May be fixed by #2781
Open

ContentDeserializer does not support 128-bit integers #2576

thill opened this issue Aug 18, 2023 · 2 comments · May be fixed by #2781

Comments

@thill
Copy link

thill commented Aug 18, 2023

ContentDeserializer does not support 128-bit integers, which causes issues when attempting to use some features with formats that support 128-bit numbers. The following tag/rename pattern fails using serde_cbor and ciborium, which accept i128-bit types, returning ErrorImpl { code: Message("i128 is not supported"), offset: 0 }.

#[test]
fn bug() {
    #[derive(serde::Deserialize, serde::Serialize)]
    struct Foo {
        message: String,
    }

    #[derive(serde::Deserialize, serde::Serialize)]
    struct Bar {
        value: i128,
    }

    #[derive(serde::Deserialize, serde::Serialize)]
    #[serde(tag = "ty")]
    enum MyEnum {
        #[serde(rename = "foo")]
        Foo(Foo),
        #[serde(rename = "bar")]
        Bar(Bar),
    }

    // this works (decode MyEnum::Foo)
    {
        let v = MyEnum::Foo(Foo {
            message: "hello, world!".to_owned(),
        });
        let encoded = serde_cbor::to_vec(&v).unwrap();
        let decoded: MyEnum = serde_cbor::from_slice(&encoded).unwrap();
        match decoded {
            MyEnum::Foo(v) => assert_eq!(&v.message, "hello, world!"),
            _ => assert!(false),
        }
    }

    // this works (decode Bar directly)
    {
        let v = MyEnum::Bar(Bar {
            value: 12345678901234567890,
        });
        let encoded = serde_cbor::to_vec(&v).unwrap();
        let decoded: Bar = serde_cbor::from_slice(&encoded).unwrap();
        assert_eq!(decoded.value, 12345678901234567890);
    }

    // this fails (decode MyEnum::Bar), i128 not supported
    {
        let v = MyEnum::Bar(Bar {
            value: 12345678901234567890,
        });
        let encoded = serde_cbor::to_vec(&v).unwrap();
        let decoded: MyEnum = serde_cbor::from_slice(&encoded).unwrap();
        match decoded {
            MyEnum::Bar(v) => assert_eq!(v.value, 12345678901234567890),
            _ => assert!(false),
        }
    }
}
@koehlma
Copy link

koehlma commented Aug 20, 2023

For reference, PR #2348 does include support for 128-bit integers.

@juntyr
Copy link

juntyr commented Sep 10, 2023

@dtolnay Since #2600 landed recently, I wonder if not including u128/i128 in the ser and de content types was an oversight?

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

Successfully merging a pull request may close this issue.

3 participants