Skip to content

api proposal: add Result return value to Encode #288

Open
@darkskygit

Description

@darkskygit

Motivation

When encoding crdt items into binary, unexpected data may be encountered. At this time, yrs will interrupt the execution process through panic! like this:

panic!("Couldn't get item's parent")

The panic will cause the execution thread to crash, which makes the developer need to isolate the relevant calls of yrs through catch_unwind or other means in actual development.

Rust provides Result abstraction, which allows us to deal with errors more gently.

Currently, the Decode trait already supports returning Result, so developers can safely handle decoding errors when encountering erroneous data:

pub trait Decode: Sized {
fn decode<D: Decoder>(decoder: &mut D) -> Result<Self, Error>;

Use Case

Before:

let doc = Doc::new();

// do some complex operations

// encode to binary, no error message, only panic when something goes wrong
let data = doc.transact().state_vector().encode_v1();

After:

let doc = Doc::new();

// do some complex operations

// encode to binary, catch error through `Result`
let data = doc.transact().state_vector().encode_v1()?;
// or
match doc.transact().state_vector().encode_v1() {
    Ok(data) => {
        // on success
    },
    Err(e) => {
        // on error
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions