-
Notifications
You must be signed in to change notification settings - Fork 52
new literacy::{Read, Write} traits to handle std/no_std (alternative) #127
Changes from 2 commits
f693c4b
41e04db
4291b4b
73c4aad
62855e6
8bf2f9e
12580fd
9e49b27
a37f2be
a8eb19c
1f500f1
6b7b28f
c6d4ca4
38bb903
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
#[cfg(all(feature = "std", feature = "use-core2"))] | ||
compile_error!("feature \"std\" and \"use-core2\" cannot be enabled together."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should "default" to core2 in this case - if we already are building against the core2 dep, we might as well just assume the user set core2/std and then the core2 impl is the same as doing the std impl. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree but there are some implementation issue, in particular
How so? with another feature There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. like you said in chat....
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
We don't have to specify it ourselves for core2/std to be enabled by some other dependency path. Its probably neater to just impl for std and not core2 than to compile-error, and if users intended to use core2 they can always fix it by, themselves, directly requiring core2/std. This avoids the issue of having some crate that depends on A and B and A enables std and B enables core2, giving the user no way to fix the issue aside from patching either A or B. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, removed the |
||
|
||
|
||
pub trait Read{ | ||
type Error; | ||
fn read(&mut self, buf: &mut [u8]) -> ::core::result::Result<usize, Self::Error>; | ||
} | ||
|
||
pub trait Write { | ||
type Error; | ||
fn write(&mut self, buf: &[u8]) -> ::core::result::Result<usize, Self::Error>; | ||
fn flush(&mut self) -> ::core::result::Result<(), Self::Error>; | ||
fn write_all(&mut self, mut buf: &[u8]) -> ::core::result::Result<(), Self::Error> { | ||
while !buf.is_empty() { | ||
match self.write(buf) { | ||
/*Ok(0) => { | ||
return Err(Error::new(ErrorKind::WriteZero, "failed to write whole buffer")); | ||
}*/ | ||
Ok(n) => buf = &buf[n..], | ||
//Err(ref e) if e.kind() == ErrorKind::Interrupted => {} | ||
Err(e) => return Err(e), | ||
} | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(feature = "std")] | ||
mod std_impl { | ||
use super::{Read, Write}; | ||
|
||
impl<R: ::std::io::Read> Read for R { | ||
sgeisler marked this conversation as resolved.
Show resolved
Hide resolved
|
||
type Error = ::std::io::Error; | ||
|
||
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> { | ||
Ok(<Self as ::std::io::Read>::read(self, buf)?) | ||
} | ||
} | ||
|
||
impl<W: ::std::io::Write> Write for W { | ||
type Error = ::std::io::Error; | ||
|
||
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> { | ||
<Self as ::std::io::Write>::write(self, buf) | ||
} | ||
|
||
fn flush(&mut self) -> Result<(), Self::Error> { | ||
<Self as ::std::io::Write>::flush(self) | ||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "use-core2")] | ||
mod core2_impl { | ||
use super::{Read, Write}; | ||
|
||
impl<R: core2::io::Read> Read for R { | ||
type Error = core2::io::Error; | ||
|
||
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> { | ||
Ok(<Self as core2::io::Read>::read(self, buf)?) | ||
} | ||
} | ||
|
||
impl<W: core2::io::Write> Write for W { | ||
type Error = core2::io::Error; | ||
|
||
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> { | ||
Ok(<Self as core2::io::Write>::write(self, buf)?) | ||
} | ||
|
||
fn flush(&mut self) -> Result<(), Self::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]); | ||
} | ||
} | ||
} |
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 hope you didn't intend to land this :)
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.
removed, and added doc where missing in 8bf2f9e