Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Remove more as conversions in protocol::messages #121

Merged
merged 3 commits into from
Mar 24, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/protocol/messages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ fn read_versioned_array<R: Read, T: ReadVersionedType<R>>(
format!("Invalid negative length for array: {}", l).into(),
))),
_ => {
let len = len as usize;
let len = usize::try_from(len).map_err(ReadError::Overflow)?;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be .map_err(|e| ReadVersionedError::ReadError(ReadError::Overflow(e)))??

Copy link
Collaborator

@crepererum crepererum Mar 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think thiserror and ? take care of that:

#[derive(Error, Debug)]
pub enum ReadVersionedError {
#[error("Read error: {0}")]
ReadError(#[from] ReadError),
}

See the #[from] macro. Also note that ? doesn't mean "return this error" but "return this error, rustc might apply a From/Into conversion. So

let x = f()?;

is more or less

let x = match f() {
    Ok(x) => x,
    Err(e) => {
        return e.into();
    }
};

let mut builder = VecBuilder::new(len);
for _ in 0..len {
builder.push(T::read_versioned(reader, version)?);
Expand Down Expand Up @@ -187,7 +187,8 @@ fn read_compact_versioned_array<R: Read, T: ReadVersionedType<R>>(
match len {
0 => Ok(None),
n => {
let len = (n - 1) as usize;
let len = usize::try_from(n - 1)
.map_err(|e| ReadVersionedError::ReadError(ReadError::Overflow(e)))?;
pierwill marked this conversation as resolved.
Show resolved Hide resolved
let mut builder = VecBuilder::new(len);
for _ in 0..len {
builder.push(T::read_versioned(reader, version)?);
Expand Down