1

I'm given a data-format that includes a sequence of objects with exactly one named field value each. Can I remove this layer of indirection while deserializing?

When deserializing, the natural representation would be

/// Each record has it's own `{ value: ... }` object
#[derive(serde::Deserialize)]
struct Foobar<T> {
    value: T,
}

/// The naive representation, via `Foobar`...
#[derive(serde::Deserialize)]
struct FoobarContainer {
    values: Vec<Foobar<T>>,
}

While Foobar adds no extra cost beyond T, I'd like to remove this layer of indirection at the type-level:

#[derive(serde::Deserialize)]
struct FoobarContainer {
    values: Vec<T>,
}

Can Foobar be removed from FoobarContainer, while still using it using deserialization?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
user2722968
  • 13,636
  • 2
  • 46
  • 67
  • @shepmaster, the problem comes with `Vec<...>`, so I need/want to delegate to the `Vec`-deserializer. `deserialize_with="Vec>"` is not legal syntax – user2722968 Oct 21 '19 at 19:20

1 Answers1

3

In the general case, there's no trivial way to make this transformation. For that, review these existing answers:

The first is my normal go-to solution and looks like this in this example.


However, in your specific case, you say:

objects with exactly one named field value

And you've identified a key requirement:

While Foobar adds no extra cost beyond T

This means that you can make Foobar have a transparent representation and use unsafe Rust to transmute between the types (although not actually with mem::transmute):

struct FoobarContainer<T> {
    values: Vec<T>,
}

#[derive(serde::Deserialize)]
#[repr(transparent)]
struct Foobar<T> {
    value: T,
}

impl<'de, T> serde::Deserialize<'de> for FoobarContainer<T>
where
    T: serde::Deserialize<'de>,
{
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let mut v: Vec<Foobar<T>> = serde::Deserialize::deserialize(deserializer)?;

        // I copied this from Stack Overflow without reading the surrounding
        // text that describes why this is actually safe.
        let values = unsafe {
            let data = v.as_mut_ptr() as *mut T;
            let len = v.len();
            let cap = v.capacity();

            std::mem::forget(v);

            Vec::from_raw_parts(data, len, cap)
        };

        Ok(FoobarContainer { values })
    }
}

See also:

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366