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 2 commits
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
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 { | ||
Wrapped(::alloc::boxed::Box<dyn Marker>), | ||
|
||
// Needed for write_all blanket implementation | ||
WriteZero, | ||
Interrupted, | ||
} | ||
|
||
pub trait Marker: ::core::fmt::Display + ::core::fmt::Debug {} | ||
|
||
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, Marker}; | ||
|
||
impl Marker for ::std::io::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::Wrapped(::std::boxed::Box::new(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, Marker}; | ||
|
||
impl Marker for core2::io::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::Wrapped(::alloc::boxed::Box::new(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.
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 eitherstd
orcore2
) 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
?