From 4e18caa5f359ffe68eaaffd0cff28ec8625367e5 Mon Sep 17 00:00:00 2001 From: LinkTed Date: Wed, 28 Jul 2021 20:43:08 +0200 Subject: [PATCH] io: Add the Peek trait Add the trait for each struct which has a peek function. --- library/std/src/io/mod.rs | 12 ++++++++++++ library/std/src/io/prelude.rs | 2 +- library/std/src/net/tcp.rs | 7 +++++++ library/std/src/net/udp.rs | 9 ++++++++- library/std/src/os/unix/net/datagram.rs | 9 ++++++++- library/std/src/os/unix/net/stream.rs | 7 +++++++ 6 files changed, 43 insertions(+), 3 deletions(-) diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs index f8ebbe1cba721..eda37a712253f 100644 --- a/library/std/src/io/mod.rs +++ b/library/std/src/io/mod.rs @@ -445,6 +445,18 @@ pub(crate) fn default_read_exact(this: &mut R, mut buf: &mut [ } } +/// The `Peek` trait allows for reading bytes from a source, without removing that data from +/// the queue. +/// +/// This trait behaves like [`Read`], except the bytes are not removed from the source. +#[unstable(feature = "peek_trait", issue = "none")] +pub trait Peek { + /// This function behaves like [`Read::read`], except the bytes are not removed from the + /// source. + #[unstable(feature = "peek_trait", issue = "none")] + fn peek(&mut self, buf: &mut [u8]) -> Result; +} + /// The `Read` trait allows for reading bytes from a source. /// /// Implementors of the `Read` trait are called 'readers'. diff --git a/library/std/src/io/prelude.rs b/library/std/src/io/prelude.rs index d80643101f2ed..a6f30721e8ba6 100644 --- a/library/std/src/io/prelude.rs +++ b/library/std/src/io/prelude.rs @@ -11,4 +11,4 @@ #![stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")] -pub use super::{BufRead, Read, Seek, Write}; +pub use super::{BufRead, Peek, Read, Seek, Write}; diff --git a/library/std/src/net/tcp.rs b/library/std/src/net/tcp.rs index 223726d45d72a..bab4881eb780e 100644 --- a/library/std/src/net/tcp.rs +++ b/library/std/src/net/tcp.rs @@ -605,6 +605,13 @@ impl TcpStream { } } +#[unstable(feature = "peek_trait", issue = "none")] +impl Peek for TcpStream { + fn peek(&mut self, buf: &mut [u8]) -> io::Result { + self.0.peek(buf) + } +} + // In addition to the `impl`s here, `TcpStream` also has `impl`s for // `AsFd`/`From`/`Into` and // `AsRawFd`/`IntoRawFd`/`FromRawFd`, on Unix and WASI, and diff --git a/library/std/src/net/udp.rs b/library/std/src/net/udp.rs index 6354752e64e76..fb301782c36d8 100644 --- a/library/std/src/net/udp.rs +++ b/library/std/src/net/udp.rs @@ -2,7 +2,7 @@ mod tests; use crate::fmt; -use crate::io::{self, Error, ErrorKind}; +use crate::io::{self, Error, ErrorKind, Peek}; use crate::net::{Ipv4Addr, Ipv6Addr, SocketAddr, ToSocketAddrs}; use crate::sys_common::net as net_imp; use crate::sys_common::{AsInner, FromInner, IntoInner}; @@ -779,6 +779,13 @@ impl UdpSocket { } } +#[unstable(feature = "peek_trait", issue = "none")] +impl Peek for UdpSocket { + fn peek(&mut self, buf: &mut [u8]) -> io::Result { + self.0.peek(buf) + } +} + // In addition to the `impl`s here, `UdpSocket` also has `impl`s for // `AsFd`/`From`/`Into` and // `AsRawFd`/`IntoRawFd`/`FromRawFd`, on Unix and WASI, and diff --git a/library/std/src/os/unix/net/datagram.rs b/library/std/src/os/unix/net/datagram.rs index f11eec18cc521..07d6a8f098949 100644 --- a/library/std/src/os/unix/net/datagram.rs +++ b/library/std/src/os/unix/net/datagram.rs @@ -19,7 +19,7 @@ use super::{sockaddr_un, SocketAddr}; target_os = "netbsd", target_os = "openbsd", ))] -use crate::io::{IoSlice, IoSliceMut}; +use crate::io::{IoSlice, IoSliceMut, Peek}; use crate::net::Shutdown; use crate::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd}; use crate::path::Path; @@ -877,6 +877,13 @@ impl UnixDatagram { } } +#[unstable(feature = "peek_trait", issue = "none")] +impl Peek for UnixDatagram { + fn peek(&mut self, buf: &mut [u8]) -> io::Result { + self.0.peek(buf) + } +} + #[stable(feature = "unix_socket", since = "1.10.0")] impl AsRawFd for UnixDatagram { #[inline] diff --git a/library/std/src/os/unix/net/stream.rs b/library/std/src/os/unix/net/stream.rs index 4119de3c03cbe..28a685928da11 100644 --- a/library/std/src/os/unix/net/stream.rs +++ b/library/std/src/os/unix/net/stream.rs @@ -573,6 +573,13 @@ impl UnixStream { } } +#[unstable(feature = "peek_trait", issue = "none")] +impl io::Peek for UnixStream { + fn peek(&mut self, buf: &mut [u8]) -> io::Result { + self.0.peek(buf) + } +} + #[stable(feature = "unix_socket", since = "1.10.0")] impl io::Read for UnixStream { fn read(&mut self, buf: &mut [u8]) -> io::Result {