Skip to content

Commit 4e18caa

Browse files
committed
io: Add the Peek trait
Add the trait for each struct which has a peek function.
1 parent 6e12110 commit 4e18caa

File tree

6 files changed

+43
-3
lines changed

6 files changed

+43
-3
lines changed

library/std/src/io/mod.rs

+12
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,18 @@ pub(crate) fn default_read_exact<R: Read + ?Sized>(this: &mut R, mut buf: &mut [
445445
}
446446
}
447447

448+
/// The `Peek` trait allows for reading bytes from a source, without removing that data from
449+
/// the queue.
450+
///
451+
/// This trait behaves like [`Read`], except the bytes are not removed from the source.
452+
#[unstable(feature = "peek_trait", issue = "none")]
453+
pub trait Peek {
454+
/// This function behaves like [`Read::read`], except the bytes are not removed from the
455+
/// source.
456+
#[unstable(feature = "peek_trait", issue = "none")]
457+
fn peek(&mut self, buf: &mut [u8]) -> Result<usize>;
458+
}
459+
448460
/// The `Read` trait allows for reading bytes from a source.
449461
///
450462
/// Implementors of the `Read` trait are called 'readers'.

library/std/src/io/prelude.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
#![stable(feature = "rust1", since = "1.0.0")]
1212

1313
#[stable(feature = "rust1", since = "1.0.0")]
14-
pub use super::{BufRead, Read, Seek, Write};
14+
pub use super::{BufRead, Peek, Read, Seek, Write};

library/std/src/net/tcp.rs

+7
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,13 @@ impl TcpStream {
605605
}
606606
}
607607

608+
#[unstable(feature = "peek_trait", issue = "none")]
609+
impl Peek for TcpStream {
610+
fn peek(&mut self, buf: &mut [u8]) -> io::Result<usize> {
611+
self.0.peek(buf)
612+
}
613+
}
614+
608615
// In addition to the `impl`s here, `TcpStream` also has `impl`s for
609616
// `AsFd`/`From<OwnedFd>`/`Into<OwnedFd>` and
610617
// `AsRawFd`/`IntoRawFd`/`FromRawFd`, on Unix and WASI, and

library/std/src/net/udp.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
mod tests;
33

44
use crate::fmt;
5-
use crate::io::{self, Error, ErrorKind};
5+
use crate::io::{self, Error, ErrorKind, Peek};
66
use crate::net::{Ipv4Addr, Ipv6Addr, SocketAddr, ToSocketAddrs};
77
use crate::sys_common::net as net_imp;
88
use crate::sys_common::{AsInner, FromInner, IntoInner};
@@ -779,6 +779,13 @@ impl UdpSocket {
779779
}
780780
}
781781

782+
#[unstable(feature = "peek_trait", issue = "none")]
783+
impl Peek for UdpSocket {
784+
fn peek(&mut self, buf: &mut [u8]) -> io::Result<usize> {
785+
self.0.peek(buf)
786+
}
787+
}
788+
782789
// In addition to the `impl`s here, `UdpSocket` also has `impl`s for
783790
// `AsFd`/`From<OwnedFd>`/`Into<OwnedFd>` and
784791
// `AsRawFd`/`IntoRawFd`/`FromRawFd`, on Unix and WASI, and

library/std/src/os/unix/net/datagram.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use super::{sockaddr_un, SocketAddr};
1919
target_os = "netbsd",
2020
target_os = "openbsd",
2121
))]
22-
use crate::io::{IoSlice, IoSliceMut};
22+
use crate::io::{IoSlice, IoSliceMut, Peek};
2323
use crate::net::Shutdown;
2424
use crate::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
2525
use crate::path::Path;
@@ -877,6 +877,13 @@ impl UnixDatagram {
877877
}
878878
}
879879

880+
#[unstable(feature = "peek_trait", issue = "none")]
881+
impl Peek for UnixDatagram {
882+
fn peek(&mut self, buf: &mut [u8]) -> io::Result<usize> {
883+
self.0.peek(buf)
884+
}
885+
}
886+
880887
#[stable(feature = "unix_socket", since = "1.10.0")]
881888
impl AsRawFd for UnixDatagram {
882889
#[inline]

library/std/src/os/unix/net/stream.rs

+7
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,13 @@ impl UnixStream {
573573
}
574574
}
575575

576+
#[unstable(feature = "peek_trait", issue = "none")]
577+
impl io::Peek for UnixStream {
578+
fn peek(&mut self, buf: &mut [u8]) -> io::Result<usize> {
579+
self.0.peek(buf)
580+
}
581+
}
582+
576583
#[stable(feature = "unix_socket", since = "1.10.0")]
577584
impl io::Read for UnixStream {
578585
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {

0 commit comments

Comments
 (0)