This repository was archived by the owner on Nov 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
new literacy::{Read, Write} traits to handle std/no_std #126
Closed
Closed
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
#[cfg(all(feature = "std", feature = "use-core2"))] | ||
compile_error!("feature \"std\" and \"use-core2\" cannot be enabled together."); | ||
|
||
#[derive(Debug)] | ||
pub enum Error { | ||
#[cfg(feature = "std")] | ||
Std(::std::io::Error), | ||
|
||
#[cfg(feature = "use-core2")] | ||
Core2(core2::io::Error), | ||
|
||
// Needed for write_all blanket implementation | ||
WriteZero, | ||
Interrupted, | ||
|
||
Other, | ||
} | ||
|
||
pub trait Read{ | ||
fn read(&mut self, buf: &mut [u8]) -> ::core::result::Result<usize, Error>; | ||
} | ||
|
||
pub trait Write { | ||
fn write(&mut self, buf: &[u8]) -> ::core::result::Result<usize, Error>; | ||
fn flush(&mut self) -> ::core::result::Result<(), Error>; | ||
|
||
fn write_all(&mut self, mut buf: &[u8]) -> ::core::result::Result<(), Error> { | ||
while !buf.is_empty() { | ||
match self.write(buf) { | ||
Ok(0) => { | ||
return Err(Error::WriteZero); | ||
} | ||
Ok(n) => buf = &buf[n..], | ||
Err(Error::Interrupted) => {} | ||
Err(e) => return Err(e), | ||
} | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(feature = "std")] | ||
mod std_impl { | ||
use super::{Read, Write, Error}; | ||
|
||
impl From<::std::io::Error> for Error { | ||
fn from(error: ::std::io::Error) -> Self { | ||
if let ::std::io::ErrorKind::Interrupted = error.kind() { | ||
Error::Interrupted | ||
} else { | ||
Error::Std(error) | ||
} | ||
} | ||
} | ||
|
||
impl<R: ::std::io::Read> Read for R { | ||
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error> { | ||
Ok(<Self as ::std::io::Read>::read(self, buf)?) | ||
} | ||
} | ||
|
||
impl<W: ::std::io::Write> Write for W { | ||
fn write(&mut self, buf: &[u8]) -> Result<usize, Error> { | ||
Ok(<Self as ::std::io::Write>::write(self, buf)?) | ||
} | ||
|
||
fn flush(&mut self) -> Result<(), Error> { | ||
Ok(<Self as ::std::io::Write>::flush(self)?) | ||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "use-core2")] | ||
mod core2_impl { | ||
use super::{Read, Write, Error}; | ||
|
||
impl From<core2::io::Error> for Error { | ||
fn from(error: core2::io::Error) -> Self { | ||
if let core2::io::ErrorKind::Interrupted = error.kind() { | ||
Error::Interrupted | ||
} else { | ||
Error::Core2(error) | ||
} | ||
} | ||
} | ||
|
||
impl<R: core2::io::Read> Read for R { | ||
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error> { | ||
Ok(<Self as core2::io::Read>::read(self, buf)?) | ||
} | ||
} | ||
|
||
impl<W: core2::io::Write> Write for W { | ||
fn write(&mut self, buf: &[u8]) -> Result<usize, Error> { | ||
Ok(<Self as core2::io::Write>::write(self, buf)?) | ||
} | ||
|
||
fn flush(&mut self) -> Result<(), Error> { | ||
Ok(<Self as core2::io::Write>::flush(self)?) | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
|
||
#[cfg(feature = "std")] | ||
mod std_test { | ||
use ::literacy::{Read, Write}; | ||
|
||
#[test] | ||
fn test_std_read() { | ||
let mut cursor = ::std::io::Cursor::new(vec![10u8]); | ||
let mut buf = [0u8; 1]; | ||
cursor.read(&mut buf).unwrap(); | ||
assert_eq!(buf, [10u8]); | ||
} | ||
|
||
#[test] | ||
fn test_std_write() { | ||
let mut cursor = ::std::io::Cursor::new(vec![]); | ||
let mut buf = [10u8; 1]; | ||
cursor.write(&mut buf).unwrap(); | ||
assert_eq!(cursor.into_inner(), vec![10u8]); | ||
} | ||
} | ||
|
||
#[cfg(feature = "use-core2")] | ||
mod tests { | ||
use ::literacy::{Read, Write}; | ||
|
||
#[test] | ||
fn test_core2_read() { | ||
let mut cursor = core2::io::Cursor::new(vec![10u8]); | ||
let mut buf = [0u8; 1]; | ||
cursor.read(&mut buf).unwrap(); | ||
assert_eq!(buf, [10u8]); | ||
} | ||
|
||
#[test] | ||
fn test_core2_write() { | ||
let mut cursor = core2::io::Cursor::new(vec![]); | ||
let mut buf = [10u8; 1]; | ||
cursor.write(&mut buf).unwrap(); | ||
assert_eq!(cursor.into_inner(), vec![10u8]); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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 theOther
variant, but I am not sure whatSomeNonStdErrorTrait
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.29There 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 onnot(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)Uh oh!
There was an error while loading. Please reload this page.
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.