Skip to content

Commit 9bd2a78

Browse files
committed
Fixes alloc::io by removing use String instead complex Error parameter.
1 parent 4007d52 commit 9bd2a78

File tree

3 files changed

+88
-79
lines changed

3 files changed

+88
-79
lines changed

library/alloc/src/io/buffered/mod.rs

-8
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,6 @@ impl<W> From<IntoInnerError<W>> for Error {
134134
}
135135
}
136136

137-
#[stable(feature = "rust1", since = "1.0.0")]
138-
impl<W: Send + fmt::Debug> error::Error for IntoInnerError<W> {
139-
#[allow(deprecated, deprecated_in_future)]
140-
fn description(&self) -> &str {
141-
error::Error::description(self.error())
142-
}
143-
}
144-
145137
#[stable(feature = "rust1", since = "1.0.0")]
146138
impl<W> fmt::Display for IntoInnerError<W> {
147139
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

library/alloc/src/io/error.rs

+17-59
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ enum Repr {
7474
#[derive(Debug)]
7575
struct Custom {
7676
kind: ErrorKind,
77-
error: Box<dyn error::Error + Send + Sync>,
77+
error: String,
7878
}
7979

8080
/// A list specifying general categories of I/O error.
@@ -177,6 +177,12 @@ pub enum ErrorKind {
177177
/// read.
178178
#[stable(feature = "read_exact", since = "1.6.0")]
179179
UnexpectedEof,
180+
181+
/// A marker variant that tells the compiler that users of this enum cannot
182+
/// match it exhaustively.
183+
#[doc(hidden)]
184+
#[stable(feature = "read_exact", since = "1.6.0")]
185+
__Nonexhaustive,
180186
}
181187

182188
impl ErrorKind {
@@ -200,6 +206,7 @@ impl ErrorKind {
200206
ErrorKind::Interrupted => "operation interrupted",
201207
ErrorKind::Other => "other os error",
202208
ErrorKind::UnexpectedEof => "unexpected end of file",
209+
_ => "unknown error",
203210
}
204211
}
205212
}
@@ -249,33 +256,15 @@ impl Error {
249256
#[stable(feature = "rust1", since = "1.0.0")]
250257
pub fn new<E>(kind: ErrorKind, error: E) -> Error
251258
where
252-
E: Into<Box<dyn error::Error + Send + Sync>>,
259+
E: Into<String>,
253260
{
254261
Self::_new(kind, error.into())
255262
}
256263

257-
fn _new(kind: ErrorKind, error: Box<dyn error::Error + Send + Sync>) -> Error {
264+
fn _new(kind: ErrorKind, error: String) -> Error {
258265
Error { repr: Repr::Custom(Box::new(Custom { kind, error })) }
259266
}
260267

261-
/// Returns an error representing the last OS error which occurred.
262-
///
263-
/// This function reads the value of `errno` for the target platform (e.g.
264-
/// `GetLastError` on Windows) and will return a corresponding instance of
265-
/// [`Error`] for the error code.
266-
///
267-
/// # Examples
268-
///
269-
/// ```
270-
/// use std::io::Error;
271-
///
272-
/// println!("last OS error: {:?}", Error::last_os_error());
273-
/// ```
274-
#[stable(feature = "rust1", since = "1.0.0")]
275-
pub fn last_os_error() -> Error {
276-
Error::from_raw_os_error(sys::os::errno() as i32)
277-
}
278-
279268
/// Creates a new instance of an [`Error`] from a particular OS error code.
280269
///
281270
/// # Examples
@@ -372,11 +361,11 @@ impl Error {
372361
/// }
373362
/// ```
374363
#[stable(feature = "io_error_inner", since = "1.3.0")]
375-
pub fn get_ref(&self) -> Option<&(dyn error::Error + Send + Sync + 'static)> {
364+
pub fn get_ref(&self) -> Option<&String> {
376365
match self.repr {
377366
Repr::Os(..) => None,
378367
Repr::Simple(..) => None,
379-
Repr::Custom(ref c) => Some(&*c.error),
368+
Repr::Custom(ref c) => Some(&c.error),
380369
}
381370
}
382371

@@ -443,11 +432,11 @@ impl Error {
443432
/// }
444433
/// ```
445434
#[stable(feature = "io_error_inner", since = "1.3.0")]
446-
pub fn get_mut(&mut self) -> Option<&mut (dyn error::Error + Send + Sync + 'static)> {
435+
pub fn get_mut(&mut self) -> Option<&mut String> {
447436
match self.repr {
448437
Repr::Os(..) => None,
449438
Repr::Simple(..) => None,
450-
Repr::Custom(ref mut c) => Some(&mut *c.error),
439+
Repr::Custom(ref mut c) => Some(&mut c.error),
451440
}
452441
}
453442

@@ -479,7 +468,7 @@ impl Error {
479468
/// }
480469
/// ```
481470
#[stable(feature = "io_error_inner", since = "1.3.0")]
482-
pub fn into_inner(self) -> Option<Box<dyn error::Error + Send + Sync>> {
471+
pub fn into_inner(self) -> Option<String> {
483472
match self.repr {
484473
Repr::Os(..) => None,
485474
Repr::Simple(..) => None,
@@ -508,7 +497,7 @@ impl Error {
508497
#[stable(feature = "rust1", since = "1.0.0")]
509498
pub fn kind(&self) -> ErrorKind {
510499
match self.repr {
511-
Repr::Os(code) => sys::decode_error_kind(code),
500+
Repr::Os(_code) => ErrorKind::Other,
512501
Repr::Custom(ref c) => c.kind,
513502
Repr::Simple(kind) => kind,
514503
}
@@ -521,8 +510,6 @@ impl fmt::Debug for Repr {
521510
Repr::Os(code) => fmt
522511
.debug_struct("Os")
523512
.field("code", &code)
524-
.field("kind", &sys::decode_error_kind(code))
525-
.field("message", &sys::os::error_string(code))
526513
.finish(),
527514
Repr::Custom(ref c) => fmt::Debug::fmt(&c, fmt),
528515
Repr::Simple(kind) => fmt.debug_tuple("Kind").field(&kind).finish(),
@@ -535,43 +522,14 @@ impl fmt::Display for Error {
535522
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
536523
match self.repr {
537524
Repr::Os(code) => {
538-
let detail = sys::os::error_string(code);
539-
write!(fmt, "{} (os error {})", detail, code)
525+
write!(fmt, "os error {}", code)
540526
}
541527
Repr::Custom(ref c) => c.error.fmt(fmt),
542528
Repr::Simple(kind) => write!(fmt, "{}", kind.as_str()),
543529
}
544530
}
545531
}
546532

547-
#[stable(feature = "rust1", since = "1.0.0")]
548-
impl error::Error for Error {
549-
#[allow(deprecated, deprecated_in_future)]
550-
fn description(&self) -> &str {
551-
match self.repr {
552-
Repr::Os(..) | Repr::Simple(..) => self.kind().as_str(),
553-
Repr::Custom(ref c) => c.error.description(),
554-
}
555-
}
556-
557-
#[allow(deprecated)]
558-
fn cause(&self) -> Option<&dyn error::Error> {
559-
match self.repr {
560-
Repr::Os(..) => None,
561-
Repr::Simple(..) => None,
562-
Repr::Custom(ref c) => c.error.cause(),
563-
}
564-
}
565-
566-
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
567-
match self.repr {
568-
Repr::Os(..) => None,
569-
Repr::Simple(..) => None,
570-
Repr::Custom(ref c) => c.error.source(),
571-
}
572-
}
573-
}
574-
575533
fn _assert_error_is_sync_send() {
576534
fn _is_sync_send<T: Sync + Send>() {}
577535
_is_sync_send::<Error>();

library/alloc/src/io/mod.rs

+71-12
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ pub mod prelude;
282282
mod util;
283283
use crate::{vec::Vec, string::String};
284284

285-
const DEFAULT_BUF_SIZE: usize = crate::sys_common::io::DEFAULT_BUF_SIZE;
285+
const DEFAULT_BUF_SIZE: usize = 8 * 1024;
286286

287287
struct Guard<'a> {
288288
buf: &'a mut Vec<u8>,
@@ -942,7 +942,10 @@ pub trait Read {
942942
/// Windows.
943943
#[stable(feature = "iovec", since = "1.36.0")]
944944
#[repr(transparent)]
945-
pub struct IoSliceMut<'a>(sys::io::IoSliceMut<'a>);
945+
pub struct IoSliceMut<'a> {
946+
vec: iovec,
947+
_p: PhantomData<&'a [u8]>,
948+
}
946949

947950
#[stable(feature = "iovec-send-sync", since = "1.44.0")]
948951
unsafe impl<'a> Send for IoSliceMut<'a> {}
@@ -953,7 +956,7 @@ unsafe impl<'a> Sync for IoSliceMut<'a> {}
953956
#[stable(feature = "iovec", since = "1.36.0")]
954957
impl<'a> fmt::Debug for IoSliceMut<'a> {
955958
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
956-
fmt::Debug::fmt(self.0.as_slice(), fmt)
959+
fmt::Debug::fmt(self.as_slice(), fmt)
957960
}
958961
}
959962

@@ -966,7 +969,13 @@ impl<'a> IoSliceMut<'a> {
966969
#[stable(feature = "iovec", since = "1.36.0")]
967970
#[inline]
968971
pub fn new(buf: &'a mut [u8]) -> IoSliceMut<'a> {
969-
IoSliceMut(sys::io::IoSliceMut::new(buf))
972+
IoSliceMut {
973+
vec: iovec {
974+
iov_base: buf.as_mut_ptr() as *mut c_void,
975+
iov_len: buf.len() as iov_len_t,
976+
},
977+
_p: PhantomData,
978+
}
970979
}
971980

972981
/// Advance the internal cursor of the slice.
@@ -1019,10 +1028,34 @@ impl<'a> IoSliceMut<'a> {
10191028

10201029
let bufs = &mut bufs[remove..];
10211030
if !bufs.is_empty() {
1022-
bufs[0].0.advance(n - accumulated_len)
1031+
bufs[0].advance_self(n - accumulated_len)
10231032
}
10241033
bufs
10251034
}
1035+
1036+
#[inline]
1037+
fn advance_self(&mut self, n: usize) {
1038+
if (self.vec.iov_len as usize) < n {
1039+
panic!("advancing IoSliceMut beyond its length");
1040+
}
1041+
1042+
unsafe {
1043+
self.vec.iov_len -= n as iov_len_t;
1044+
self.vec.iov_base = self.vec.iov_base.add(n);
1045+
}
1046+
}
1047+
1048+
#[inline]
1049+
fn as_slice(&self) -> &[u8] {
1050+
unsafe { slice::from_raw_parts(self.vec.iov_base as *mut u8, self.vec.iov_len as usize) }
1051+
}
1052+
1053+
#[inline]
1054+
fn as_mut_slice(&mut self) -> &mut [u8] {
1055+
unsafe {
1056+
slice::from_raw_parts_mut(self.vec.iov_base as *mut u8, self.vec.iov_len as usize)
1057+
}
1058+
}
10261059
}
10271060

10281061
#[stable(feature = "iovec", since = "1.36.0")]
@@ -1031,15 +1064,15 @@ impl<'a> Deref for IoSliceMut<'a> {
10311064

10321065
#[inline]
10331066
fn deref(&self) -> &[u8] {
1034-
self.0.as_slice()
1067+
self.as_slice()
10351068
}
10361069
}
10371070

10381071
#[stable(feature = "iovec", since = "1.36.0")]
10391072
impl<'a> DerefMut for IoSliceMut<'a> {
10401073
#[inline]
10411074
fn deref_mut(&mut self) -> &mut [u8] {
1042-
self.0.as_mut_slice()
1075+
self.as_mut_slice()
10431076
}
10441077
}
10451078

@@ -1051,7 +1084,10 @@ impl<'a> DerefMut for IoSliceMut<'a> {
10511084
#[stable(feature = "iovec", since = "1.36.0")]
10521085
#[derive(Copy, Clone)]
10531086
#[repr(transparent)]
1054-
pub struct IoSlice<'a>(sys::io::IoSlice<'a>);
1087+
pub struct IoSlice<'a> {
1088+
vec: iovec,
1089+
_p: PhantomData<&'a [u8]>,
1090+
}
10551091

10561092
#[stable(feature = "iovec-send-sync", since = "1.44.0")]
10571093
unsafe impl<'a> Send for IoSlice<'a> {}
@@ -1062,7 +1098,7 @@ unsafe impl<'a> Sync for IoSlice<'a> {}
10621098
#[stable(feature = "iovec", since = "1.36.0")]
10631099
impl<'a> fmt::Debug for IoSlice<'a> {
10641100
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1065-
fmt::Debug::fmt(self.0.as_slice(), fmt)
1101+
fmt::Debug::fmt(self.as_slice(), fmt)
10661102
}
10671103
}
10681104

@@ -1075,7 +1111,13 @@ impl<'a> IoSlice<'a> {
10751111
#[stable(feature = "iovec", since = "1.36.0")]
10761112
#[inline]
10771113
pub fn new(buf: &'a [u8]) -> IoSlice<'a> {
1078-
IoSlice(sys::io::IoSlice::new(buf))
1114+
IoSlice {
1115+
vec: iovec {
1116+
iov_base: buf.as_ptr() as *mut u8 as *mut c_void,
1117+
iov_len: buf.len() as iov_len_t,
1118+
},
1119+
_p: PhantomData,
1120+
}
10791121
}
10801122

10811123
/// Advance the internal cursor of the slice.
@@ -1127,10 +1169,27 @@ impl<'a> IoSlice<'a> {
11271169

11281170
let bufs = &mut bufs[remove..];
11291171
if !bufs.is_empty() {
1130-
bufs[0].0.advance(n - accumulated_len)
1172+
bufs[0].advance_self(n - accumulated_len)
11311173
}
11321174
bufs
11331175
}
1176+
1177+
#[inline]
1178+
fn advance_self(&mut self, n: usize) {
1179+
if (self.vec.iov_len as usize) < n {
1180+
panic!("advancing IoSlice beyond its length");
1181+
}
1182+
1183+
unsafe {
1184+
self.vec.iov_len -= n as iov_len_t;
1185+
self.vec.iov_base = self.vec.iov_base.add(n);
1186+
}
1187+
}
1188+
1189+
#[inline]
1190+
fn as_slice(&self) -> &[u8] {
1191+
unsafe { slice::from_raw_parts(self.vec.iov_base as *mut u8, self.vec.iov_len as usize) }
1192+
}
11341193
}
11351194

11361195
#[stable(feature = "iovec", since = "1.36.0")]
@@ -1139,7 +1198,7 @@ impl<'a> Deref for IoSlice<'a> {
11391198

11401199
#[inline]
11411200
fn deref(&self) -> &[u8] {
1142-
self.0.as_slice()
1201+
self.as_slice()
11431202
}
11441203
}
11451204

0 commit comments

Comments
 (0)