serde icon indicating copy to clipboard operation
serde copied to clipboard

ContentDeserializer does not support 128-bit integers

Open thill opened this issue 2 years ago • 2 comments

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),
        }
    }
}

thill avatar Aug 18 '23 16:08 thill

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

koehlma avatar Aug 20 '23 12:08 koehlma

@dtolnay Since https://github.com/serde-rs/serde/pull/2600 landed recently, I wonder if not including u128/i128 in the ser and de content types was an oversight?

juntyr avatar Sep 10 '23 10:09 juntyr