near / borsh

Binary Object Representation Serializer for Hashing

Home Page:https://borsh.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`try_to_vec` never returns Result::Err()

lexfrl opened this issue · comments

Since the writer is vector, it will never return Result::Err(). Since that we're able to not return an Result. We use the interface quite often everywhere. Right now code is bloated with unnecessary unwrap()s.

try_to_vec https://github.com/nearprotocol/borsh/blob/af0e6d87142559150dc86e14c6f469cc40c8909e/borsh-rs/borsh/src/ser/mod.rs#L11

A Vec Write implementation:
https://doc.rust-lang.org/src/std/io/impls.rs.html#339

What do you think @nearmax ?

commented

It returns error to allow custom types that have their own implementation of BorshSerialize to return error if something goes wrong. E.g. if type has inconsistent internal state that it refuses to serialize.

Closing as WAI.

E.g. if type has inconsistent internal state that it refuses to serialize.

In that case the interface should allow to specify a custom error type. But the method is not generic over Error, it returns io::Error.

use std::io::{Error, Write};
fn try_to_vec(&self) -> Result<Vec<u8>, Error>

I suggest to add

fn to_vec(&self) -> Vec<u8>

And just deprecate try_to_vec to not break the current API.

commented

In that case the interface should allow to specify a custom error type.

io::Error is widely used by Rust serializers, it should be sufficient for custom-written serialization methods too.