udoprog/musli

What is the role of an Encode that must be consumed?

oovm opened this issue · 1 comments

oovm commented

Can you open the discussion, this is not an issue, I have something I don't understand.


First of all, I don't quite understand this place, why is it necessary to create an encoder that must be consumed?

/// The encoder returned when advancing the sequence encoder.
type Encoder<'this>: Encoder<Ok = Self::Ok, Error = Self::Error>
where
Self: 'this;
/// Prepare encoding of the next element.
#[must_use = "encoders must be consumed"]
fn next(&mut self) -> Result<Self::Encoder<'_>, Self::Error>;

If I share a &mut Vec<u8> buffer, does that mean I need to pass it between various Encoders?


Second, I don't quite understand the use of mode. For example, if I want to create a compact json representation and a pretty print json representation, should I create two encoders or two modes?

Hi! Discussions is now open, but I also don't mind this being an issue.

Why encoders need to be consumed If you leave the encoder dangling it could result in incomplete encoding. An Encoder is intended to be used by calling something like encoder.encode_u32().

About modes it's easier to think of a mode as a meta attribute when deriving an Encode or Decode implementation. Anything you can configure using attributes during a derive can be different between two modes. It can be as simple as having distinct field names to turning musli(packed) on or off (as is shown in the README example) which for JSON means the struct will be treated as an "array of values".

If I share a &mut Vec buffer, does that mean I need to pass it between various Encoders?

I don't quite get what you're asking here. Could you try and explain in more detail?

I hope some of this helps!