Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

block_import: switch to Box<dyn Any> for intermediates representation#4809

Merged
bkchr merged 4 commits intomasterfrom
sp-dyn-any
Feb 3, 2020
Merged

block_import: switch to Box<dyn Any> for intermediates representation#4809
bkchr merged 4 commits intomasterfrom
sp-dyn-any

Conversation

@sorpaas
Copy link
Copy Markdown
Contributor

@sorpaas sorpaas commented Feb 3, 2020

Use built-in rust dynamic types Box<dyn Any>. This avoids the extra step of encoding/decoding.

@sorpaas sorpaas added the A0-please_review Pull request needs code review. label Feb 3, 2020
@sorpaas sorpaas requested review from andresilva and bkchr February 3, 2020 06:00
Comment thread primitives/consensus/common/src/block_import.rs Outdated
Comment thread primitives/consensus/common/src/block_import.rs Outdated
}

/// Take interemdiate by given key, and remove it from the processing list.
pub fn take_intermediate<T: 'static>(&mut self, key: &[u8]) -> Result<Box<T>, Error> {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sorry for not being clear.

What I actually wanted was this Result<Option<Box<T>>, Error>.

The Err would just be returned when the downcast fails. I think this makes it easier if at some point we need to debug this, as there is a clear distinction between not-found and wrong-type.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@bkchr The error type for downcast is Box<dyn Any>. I think the intention of the API is that:

  • If downcast succeeds, return the downcasted type in Ok.
  • If downcast fails, return the original value in Err.

So there's really no additional errors to be returned/debugged.

In take_intermediate, we mostly want to do the same thing both when the key is not found, and when the downcast fails -- stop block importing and return error, so I'd suggest we have the return type to be either Option<Box<T>> or Result<Box<T>, ConsensusError>. WDYT?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

And indeed we can distinguish not-found/wrong-type in ConsensusError -- ConsensusError::NoIntermediate, ConsensusError::IntermediateWrongType.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah, I know that downcast doesn't return an error. However in the take_intermediate case, the value will be removed and is lost.

The problem I see with Any is that &T and T give you a different TypeId and down-casting will fail: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=872b02f7968b9528140d3ec8ed7628be

And I think this is such a subtile difference, that it easily could be done wrong. Making it easier for the user to differentiate between not-found and wrong-type will really help with these kind of errors, especially when you are new to Rust.

That both should make the import fail, was nothing I wanted to question here :)

@bkchr bkchr merged commit 9195fac into master Feb 3, 2020
@bkchr bkchr deleted the sp-dyn-any branch February 3, 2020 08:55
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

A0-please_review Pull request needs code review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants