Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
65 changes: 64 additions & 1 deletion library/alloc/src/io/impls.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::boxed::Box;
use crate::io::SizeHint;
use crate::io::{self, Seek, SeekFrom, SizeHint};
#[cfg(all(not(no_rc), not(no_sync), target_has_atomic = "ptr"))]
use crate::sync::Arc;

// =============================================================================
// Forwarding implementations
Expand All @@ -18,5 +20,66 @@ impl<T> SizeHint for Box<T> {
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<S: Seek + ?Sized> Seek for Box<S> {
#[inline]
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
(**self).seek(pos)
}

#[inline]
fn rewind(&mut self) -> io::Result<()> {
(**self).rewind()
}

#[inline]
fn stream_len(&mut self) -> io::Result<u64> {
(**self).stream_len()
}

#[inline]
fn stream_position(&mut self) -> io::Result<u64> {
(**self).stream_position()
}

#[inline]
fn seek_relative(&mut self, offset: i64) -> io::Result<()> {
(**self).seek_relative(offset)
}
}

// =============================================================================
// In-memory buffer implementations

#[cfg(all(not(no_rc), not(no_sync), target_has_atomic = "ptr"))]
#[stable(feature = "io_traits_arc", since = "1.73.0")]
impl<S: Seek + ?Sized> Seek for Arc<S>
where
for<'a> &'a S: Seek,
S: crate::io::IoHandle,
{
#[inline]
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
(&**self).seek(pos)
}

#[inline]
fn rewind(&mut self) -> io::Result<()> {
(&**self).rewind()
}

#[inline]
fn stream_len(&mut self) -> io::Result<u64> {
(&**self).stream_len()
}

#[inline]
fn stream_position(&mut self) -> io::Result<u64> {
(&**self).stream_position()
}

#[inline]
fn seek_relative(&mut self, offset: i64) -> io::Result<()> {
(&**self).seek_relative(offset)
}
}
6 changes: 3 additions & 3 deletions library/alloc/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ pub use core::io::const_error;
pub use core::io::{BorrowedBuf, BorrowedCursor};
#[unstable(feature = "alloc_io", issue = "154046")]
pub use core::io::{
Chain, Cursor, Empty, Error, ErrorKind, IoSlice, IoSliceMut, Repeat, Result, Sink, Take, empty,
repeat, sink,
Chain, Cursor, Empty, Error, ErrorKind, IoSlice, IoSliceMut, Repeat, Result, Seek, SeekFrom,
Sink, Take, empty, repeat, sink,
};
#[doc(hidden)]
#[unstable(feature = "core_io_internals", reason = "exposed only for libstd", issue = "none")]
pub use core::io::{IoHandle, OsFunctions, SizeHint, chain, take};
pub use core::io::{IoHandle, OsFunctions, SizeHint, chain, stream_len_default, take};
1 change: 1 addition & 0 deletions library/alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@
#![feature(ptr_metadata)]
#![feature(raw_os_error_ty)]
#![feature(rev_into_inner)]
#![feature(seek_stream_len)]
#![feature(set_ptr_value)]
#![feature(share_trait)]
#![feature(sized_type_properties)]
Expand Down
39 changes: 38 additions & 1 deletion library/core/src/io/cursor.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::io::{self, ErrorKind, SeekFrom};

/// A `Cursor` wraps an in-memory buffer and provides it with a
/// [`Seek`] implementation.
///
Expand All @@ -21,7 +23,7 @@
/// [`File`]: ../../std/fs/struct.File.html
/// [`Read`]: ../../std/io/trait.Read.html
/// [`Write`]: ../../std/io/trait.Write.html
/// [`Seek`]: ../../std/io/trait.Seek.html
/// [`Seek`]: crate::io::Seek
/// [Vec]: ../../alloc/vec/struct.Vec.html
///
/// ```no_run
Expand Down Expand Up @@ -291,3 +293,38 @@ where
self.pos = other.pos;
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T> io::Seek for Cursor<T>
where
T: AsRef<[u8]>,
{
fn seek(&mut self, style: SeekFrom) -> io::Result<u64> {
let (base_pos, offset) = match style {
SeekFrom::Start(n) => {
self.set_position(n);
return Ok(n);
}
SeekFrom::End(n) => (self.get_ref().as_ref().len() as u64, n),
SeekFrom::Current(n) => (self.position(), n),
};
match base_pos.checked_add_signed(offset) {
Some(n) => {
self.set_position(n);
Ok(n)
}
None => Err(io::const_error!(
ErrorKind::InvalidInput,
"invalid seek to a negative or overflowing position",
)),
}
}

fn stream_len(&mut self) -> io::Result<u64> {
Ok(self.get_ref().as_ref().len() as u64)
}

fn stream_position(&mut self) -> io::Result<u64> {
Ok(self.position())
}
}
2 changes: 1 addition & 1 deletion library/core/src/io/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub type Result<T> = result::Result<T, Error>;
// FIXME(#74481): Hard-links required to link from `core` to `std`
/// [Read]: ../../std/io/trait.Read.html
/// [Write]: ../../std/io/trait.Write.html
/// [Seek]: ../../std/io/trait.Seek.html
/// [Seek]: crate::io::Seek
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_has_incoherent_inherent_impls]
pub struct Error {
Expand Down
30 changes: 29 additions & 1 deletion library/core/src/io/impls.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::io::SizeHint;
use crate::io::{self, Seek, SeekFrom, SizeHint};

// =============================================================================
// Forwarding implementations
Expand All @@ -17,6 +17,34 @@ impl<T> SizeHint for &mut T {
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<S: Seek + ?Sized> Seek for &mut S {
#[inline]
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
(**self).seek(pos)
}

#[inline]
fn rewind(&mut self) -> io::Result<()> {
(**self).rewind()
}

#[inline]
fn stream_len(&mut self) -> io::Result<u64> {
(**self).stream_len()
}

#[inline]
fn stream_position(&mut self) -> io::Result<u64> {
(**self).stream_position()
}

#[inline]
fn seek_relative(&mut self, offset: i64) -> io::Result<()> {
(**self).seek_relative(offset)
}
}

// =============================================================================
// In-memory buffer implementations

Expand Down
5 changes: 4 additions & 1 deletion library/core/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod cursor;
mod error;
mod impls;
mod io_slice;
mod seek;
mod size_hint;
mod util;

Expand All @@ -21,12 +22,14 @@ pub use self::{
cursor::Cursor,
error::{Error, ErrorKind, Result},
io_slice::{IoSlice, IoSliceMut},
seek::{Seek, SeekFrom},
util::{Chain, Empty, Repeat, Sink, Take, empty, repeat, sink},
};
#[doc(hidden)]
#[unstable(feature = "core_io_internals", reason = "exposed only for libstd", issue = "none")]
pub use self::{
error::{Custom, CustomOwner, OsFunctions},
seek::stream_len_default,
size_hint::SizeHint,
util::{chain, take},
};
Expand All @@ -46,7 +49,7 @@ pub use self::{
/// [file]: ../../std/fs/struct.File.html
/// [arc]: ../../alloc/sync/struct.Arc.html
/// [`Write`]: ../../std/io/trait.Write.html
/// [`Seek`]: ../../std/io/trait.Seek.html
/// [`Seek`]: crate::io::Seek
#[doc(hidden)]
#[unstable(feature = "core_io_internals", reason = "exposed only for libstd", issue = "none")]
pub trait IoHandle {}
Loading
Loading