aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2022-02-25 21:15:47 -0500
committerJesse Luehrs <doy@tozt.net>2022-02-25 21:15:58 -0500
commit4513e54c073ea169b98f02f330bb31dff26fee99 (patch)
treeebe5a3534435f43c424453b056dfabd373481b5b
parentf0710ededc80610fdaff3209f8b94fa0d7bb78ce (diff)
downloadpty-process-4513e54c073ea169b98f02f330bb31dff26fee99.tar.gz
pty-process-4513e54c073ea169b98f02f330bb31dff26fee99.zip
simplify and remove allocations
-rw-r--r--src/pty.rs36
1 files changed, 15 insertions, 21 deletions
diff --git a/src/pty.rs b/src/pty.rs
index a63cc38..1071df9 100644
--- a/src/pty.rs
+++ b/src/pty.rs
@@ -76,14 +76,12 @@ impl tokio::io::AsyncRead for Pty {
std::task::Poll::Ready(guard) => guard,
std::task::Poll::Pending => return std::task::Poll::Pending,
}?;
- let mut b = vec![0u8; buf.capacity()];
- match guard.try_io(|inner| (&inner.get_ref().0).read(&mut b)) {
+ // XXX should be able to optimize this once read_buf is stabilized
+ // in std
+ let b = buf.initialize_unfilled();
+ match guard.try_io(|inner| (&inner.get_ref().0).read(b)) {
Ok(Ok(bytes)) => {
- // XXX this is safe, but not particularly efficient
- buf.clear();
- buf.initialize_unfilled_to(bytes);
- buf.set_filled(bytes);
- buf.filled_mut().copy_from_slice(&b[..bytes]);
+ buf.advance(bytes);
return std::task::Poll::Ready(Ok(()));
}
Ok(Err(e)) => return std::task::Poll::Ready(Err(e)),
@@ -154,14 +152,12 @@ impl<'a> tokio::io::AsyncRead for ReadPty<'a> {
std::task::Poll::Ready(guard) => guard,
std::task::Poll::Pending => return std::task::Poll::Pending,
}?;
- let mut b = vec![0u8; buf.capacity()];
- match guard.try_io(|inner| (&inner.get_ref().0).read(&mut b)) {
+ // XXX should be able to optimize this once read_buf is stabilized
+ // in std
+ let b = buf.initialize_unfilled();
+ match guard.try_io(|inner| (&inner.get_ref().0).read(b)) {
Ok(Ok(bytes)) => {
- // XXX this is safe, but not particularly efficient
- buf.clear();
- buf.initialize_unfilled_to(bytes);
- buf.set_filled(bytes);
- buf.filled_mut().copy_from_slice(&b[..bytes]);
+ buf.advance(bytes);
return std::task::Poll::Ready(Ok(()));
}
Ok(Err(e)) => return std::task::Poll::Ready(Err(e)),
@@ -267,14 +263,12 @@ impl tokio::io::AsyncRead for OwnedReadPty {
std::task::Poll::Ready(guard) => guard,
std::task::Poll::Pending => return std::task::Poll::Pending,
}?;
- let mut b = vec![0u8; buf.capacity()];
- match guard.try_io(|inner| (&inner.get_ref().0).read(&mut b)) {
+ // XXX should be able to optimize this once read_buf is stabilized
+ // in std
+ let b = buf.initialize_unfilled();
+ match guard.try_io(|inner| (&inner.get_ref().0).read(b)) {
Ok(Ok(bytes)) => {
- // XXX this is safe, but not particularly efficient
- buf.clear();
- buf.initialize_unfilled_to(bytes);
- buf.set_filled(bytes);
- buf.filled_mut().copy_from_slice(&b[..bytes]);
+ buf.advance(bytes);
return std::task::Poll::Ready(Ok(()));
}
Ok(Err(e)) => return std::task::Poll::Ready(Err(e)),