Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converted all platform-specific stdin/stdout/stderr implementations to use io:: traits #73495

Merged
merged 3 commits into from
Jun 23, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 17 additions & 20 deletions src/libstd/sys/hermit/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,21 @@ impl Stdin {
pub fn new() -> io::Result<Stdin> {
Ok(Stdin)
}
}

pub fn read(&self, data: &mut [u8]) -> io::Result<usize> {
impl io::Read for Stdin {
fn read(&mut self, data: &mut [u8]) -> io::Result<usize> {
self.read_vectored(&mut [IoSliceMut::new(data)])
}

pub fn read_vectored(&self, _data: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
fn read_vectored(&self, _data: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
Lucretiel marked this conversation as resolved.
Show resolved Hide resolved
//ManuallyDrop::new(unsafe { WasiFd::from_raw(libc::STDIN_FILENO as u32) })
// .read(data)
Lucretiel marked this conversation as resolved.
Show resolved Hide resolved
Ok(0)
}

#[inline]
pub fn is_read_vectored(&self) -> bool {
fn is_read_vectored(&self) -> bool {
true
}
}
Expand All @@ -31,8 +33,10 @@ impl Stdout {
pub fn new() -> io::Result<Stdout> {
Ok(Stdout)
}
}

pub fn write(&self, data: &[u8]) -> io::Result<usize> {
impl io::Write for Stdout {
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
let len;

unsafe { len = abi::write(1, data.as_ptr() as *const u8, data.len()) }
Expand All @@ -44,7 +48,7 @@ impl Stdout {
}
}

pub fn write_vectored(&self, data: &[IoSlice<'_>]) -> io::Result<usize> {
fn write_vectored(&mut self, data: &[IoSlice<'_>]) -> io::Result<usize> {
let len;

unsafe { len = abi::write(1, data.as_ptr() as *const u8, data.len()) }
Expand All @@ -57,11 +61,11 @@ impl Stdout {
}

#[inline]
pub fn is_write_vectored(&self) -> bool {
fn is_write_vectored(&self) -> bool {
true
}

pub fn flush(&self) -> io::Result<()> {
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
Expand All @@ -70,8 +74,10 @@ impl Stderr {
pub fn new() -> io::Result<Stderr> {
Ok(Stderr)
}
}

pub fn write(&self, data: &[u8]) -> io::Result<usize> {
impl io::Write for Stderr {
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
let len;

unsafe { len = abi::write(2, data.as_ptr() as *const u8, data.len()) }
Expand All @@ -83,7 +89,7 @@ impl Stderr {
}
}

pub fn write_vectored(&self, data: &[IoSlice<'_>]) -> io::Result<usize> {
fn write_vectored(&mut self, data: &[IoSlice<'_>]) -> io::Result<usize> {
let len;

unsafe { len = abi::write(2, data.as_ptr() as *const u8, data.len()) }
Expand All @@ -96,21 +102,12 @@ impl Stderr {
}

#[inline]
pub fn is_write_vectored(&self) -> bool {
fn is_write_vectored(&self) -> bool {
true
}

pub fn flush(&self) -> io::Result<()> {
Ok(())
}
}

impl io::Write for Stderr {
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
(&*self).write(data)
}
fn flush(&mut self) -> io::Result<()> {
(&*self).flush()
Ok(())
}
}

Expand Down
66 changes: 32 additions & 34 deletions src/libstd/sys/wasi/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,85 +11,83 @@ impl Stdin {
Ok(Stdin)
}

pub fn read(&self, data: &mut [u8]) -> io::Result<usize> {
#[inline]
pub fn as_raw_fd(&self) -> u32 {
0
}
}

impl io::Read for Stdin {
fn read(&mut self, data: &mut [u8]) -> io::Result<usize> {
self.read_vectored(&mut [IoSliceMut::new(data)])
}

pub fn read_vectored(&self, data: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
fn read_vectored(&mut self, data: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
ManuallyDrop::new(unsafe { WasiFd::from_raw(self.as_raw_fd()) }).read(data)
}

#[inline]
pub fn is_read_vectored(&self) -> bool {
fn is_read_vectored(&self) -> bool {
true
}

pub fn as_raw_fd(&self) -> u32 {
0
}
}

impl Stdout {
pub fn new() -> io::Result<Stdout> {
Ok(Stdout)
}

pub fn write(&self, data: &[u8]) -> io::Result<usize> {
#[inline]
pub fn as_raw_fd(&self) -> u32 {
1
}
}

impl io::Write for Stdout {
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
self.write_vectored(&[IoSlice::new(data)])
}

pub fn write_vectored(&self, data: &[IoSlice<'_>]) -> io::Result<usize> {
fn write_vectored(&mut self, data: &[IoSlice<'_>]) -> io::Result<usize> {
ManuallyDrop::new(unsafe { WasiFd::from_raw(self.as_raw_fd()) }).write(data)
}

#[inline]
pub fn is_write_vectored(&self) -> bool {
fn is_write_vectored(&self) -> bool {
true
}

pub fn flush(&self) -> io::Result<()> {
fn flush(&mut self) -> io::Result<()> {
Ok(())
}

pub fn as_raw_fd(&self) -> u32 {
1
}
}

impl Stderr {
pub fn new() -> io::Result<Stderr> {
Ok(Stderr)
}

pub fn write(&self, data: &[u8]) -> io::Result<usize> {
self.write_vectored(&[IoSlice::new(data)])
}

pub fn write_vectored(&self, data: &[IoSlice<'_>]) -> io::Result<usize> {
ManuallyDrop::new(unsafe { WasiFd::from_raw(self.as_raw_fd()) }).write(data)
}

#[inline]
pub fn is_write_vectored(&self) -> bool {
true
}

pub fn flush(&self) -> io::Result<()> {
Ok(())
}

pub fn as_raw_fd(&self) -> u32 {
2
}
}

impl io::Write for Stderr {
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
(&*self).write(data)
self.write_vectored(&[IoSlice::new(data)])
}

fn write_vectored(&mut self, data: &[IoSlice<'_>]) -> io::Result<usize> {
ManuallyDrop::new(unsafe { WasiFd::from_raw(self.as_raw_fd()) }).write(data)
}

#[inline]
fn is_write_vectored(&self) -> bool {
true
}

fn flush(&mut self) -> io::Result<()> {
(&*self).flush()
Ok(())
}
}

Expand Down