fury icon indicating copy to clipboard operation
fury copied to clipboard

[Rust] Support deserialize by populate passed mut ref

Open chaokunyang opened this issue 3 months ago • 2 comments

Feature Request

Currently fory rust provide following deserialize API:

    pub fn deserialize<T: Serializer>(&self, bf: &[u8]) -> Result<T, Error> {
        let mut reader = Reader::new(bf);
        let meta_offset = self.read_head(&mut reader)?;
        let mut context = ReadContext::new(self, reader);
        if meta_offset > 0 {
            context.load_meta(meta_offset as usize);
        }
        <T as Serializer>::deserialize(&mut context)
    }

It relies on compiler Return Value Optimization (RVO), which won't work always. And the compiler will copy small struct always.

Is your feature request related to a problem? Please describe

pub fn deserialize<T: Serializer>(&self, bf: &[u8]) -> Result<T, Error> API may introduce extra copy.

Describe the solution you'd like

Add a new API to populate passed struct directly.

pub fn deserialize_into<T: Serializer>(&self, bf: &[u8], output: &mut T) -> Result<(), Error> {
    let mut reader = Reader::new(bf);
    let meta_offset = self.read_head(&mut reader)?;
    let mut context = ReadContext::new(self, reader);
    if meta_offset > 0 {
        context.load_meta(meta_offset as usize);
    }
    <T as Serializer>::deserialize_into(&mut context, output)
}

Describe alternatives you've considered

No response

Additional context

No response

chaokunyang avatar Aug 29 '25 07:08 chaokunyang

I want to try this, please assign it to me

Tinuvile avatar Oct 10 '25 06:10 Tinuvile

Great! assigned to you

chaokunyang avatar Oct 10 '25 06:10 chaokunyang