earthstar-project / willow-rs

Implementations of the Willow family of specifications in Rust.
https://willowprotocol.org
Apache License 2.0
7 stars 0 forks source link

Encoding and decoding #3

Open AljoschaMeyer opened 3 weeks ago

AljoschaMeyer commented 3 weeks ago
// Encoding functions should have the following form:

pub fn encode_usize<Con: BulkConsumer<Item = u8>>(value: &usize, consumer: &mut Con) -> Result<(), Con::Error> {
    unimplemented!()
}

// To implement those, you'll need some helper functions for working with ufotofu. For example, you'll probably want the following:

pub fn consume_all<Item, Con: BulkConsumer<Item=Item>>(values: &Item[], consumer: &mut Con) -> Result<(), Con::Error> {
    unimplemented!()
}

// And, of course, there'll be nb_local versons of all of these as well (with their own helper functions, too):

pub async fn nb_local_encode_usize<Con: nb_local::BulkConsumer<Item = u8>>(value: &usize, consumer: &mut Con) -> Result<(), Con::Error> {
    unimplemented!()
}

// Note that these designs assume that encoding is infallible, which is not necessarily true in the greater rust ecosystem (for example, a poisoned mutex cannot be encoded with serde). I prefer the design of "every instance of a type is valid" and "every valid value can be encoded", hence no need for encoding errors.

// For decoding, things can obviously go wrong. Either because the data source errors, or because the data source produces garbage.
pub enum DecodeError<ProducerError> {
    ProducerErr(ProducerError),
    GarbageInput,
}

// Decoding functions should have the following form:

pub fn decode_usize<Pro: BulkProducer<Item = u8>>(producer: &mut Pro) -> Result<u8, DecodeError<Pro::Error>> {
    unimplemented!()
}

// And async:

pub async fn nb_local_decode_usize<Pro: nb_local::BulkProducer<Item = u8>>(producer: &mut Pro) -> Result<u8, DecodeError<Pro::Error>> {
    unimplemented!()
}

General encoding/decoding utilities should live in some (the?) ufotofu create.

mycognosist commented 3 weeks ago

Are you suggesting that consume_all() and local_nb_consume_all() live in ufotofu and all the other methods are implemented in Willow?