-
Notifications
You must be signed in to change notification settings - Fork 52
new literacy::{Read, Write} traits to handle std/no_std #126
Conversation
The advantage of this is that the library user can supply their own implementation of the literacy traits, which seems desirable for |
Won't there be an issue compiling with the rust-bitcoin MSRV, which is 1.29? The |
my understanding is with |
oh, of course |
src/literacy.rs
Outdated
#[derive(Debug)] | ||
pub enum Error { | ||
#[cfg(feature = "std")] | ||
Std(::std::io::Error), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feature-gated enums are really bad because cargo uses the union set of all features in the dependency tree. E.g. if there are two deps with a rust-bitcoin dep and one rust-bitcoin has use-core and another std, cargo will compile it once with both features. That means a non-breaking change (just changing the feature set of rust-bitcoin without exposing it) in one of the deps could break exhaustive matches.
Would Box<dyn SomeNonStdErrorTrait>
work here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought of Box<dyn SomeNonStdErrorTrait>
for the Other
variant, but I am not sure what SomeNonStdErrorTrait
could be, Debug
? Or something created ad-hoc?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could be a new trait defined as trait OurErrorTrait: Dsiplay + Debug + … {}
, essentially just a hack to make multiple trait bounds object safe.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can't avoid the feature gate on the error variant on 1.29, because you can't use Box
taken from alloc on 1.29
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With 1.29, you must turn on std and must not turn on use-core2 anyway, so there seems to be no issue. Box only needs to be imported from alloc
conditional on not(feature = "std"))
, which will be skipped by 1.29.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, right, I had to update the CI for that (skipping no_std for 1.29) 4727b63
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a bit confused as to how CI was passing before, given that 1.29 can't compile core2.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was not compiling it, features in contrib/test.sh
were not updated (there were also 1 feature not existing anymore)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The two features are mutually exclusive right? (which by itself also doesn't play well with cargo's way of handling features)
Instead of having conditionally defined variants, you could have a single variant and conditionally define its private, inner field?
I think that should address @sgeisler's concern.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like where this is going, I think the Any
change would be super nice to have in here if possible, so not a full ACK yet. And someone should have a thorough look at the CI changes so we don't break it again (I'm not in the mood for bash right now though).
} | ||
|
||
pub trait Marker: ::core::fmt::Display + ::core::fmt::Debug {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should definitely include Any
here if possible. This would allow a user that knows which error they expect (typical use case: you use either std
or core2
) to downcast to that error for further introspection. Maybe we'd even want to provide a helper method for that.
Also: I hope we can come up with a better name, it's not really a marker trait imo. Maybe call it LiteracyError
?
What is the best place for this, given that it could be used by both rust-bitcoin (for consensus encoding) and bitcoin_hashes? Should it be a new repo? |
Good question! I don't have any strong opinion here, just practically it could stay here as rust-bitcoin already depends on bitcoin_hashes. If we have enough to put in it a new crate isn't terrible, but given its pretty small I kinda figure just leave it here? |
But if it's here, then bitcoin_hashes can't use it. |
here is bitcoin_hashes? |
Sorry, misread that. That's fine for the time being. |
I think it would be a problem having this here if libraries at the same level as |
I think moving as much logic as possible inside github action small job instead of the The downside of this it that is surely less friendly to launch in local env |
closing in favor of #127 |
This follows the discussion in #120
introduce traits
literacy::{Read, Write}
that are:std::io::{Read, Write}
core2::io::{Read, Write}
This is the first proposal, an alternative one is following along:
literacy::Error
with variants and provides thewrite_all
blanket methods. The downside of this impl is that custom implementation can't have custom errorswrite_all
implementation doesn't cover all pathNot yet sure which one is better or can be improved.
Also not sure if the approach followed in rust-bitcoin/rust-bitcoin#603 is better and make this useless, but I had already partially done this so here it is.