Skip to content

Commit 74d53b5

Browse files
committed
Fixes alloc::io by removing use String instead complex Error parameter.
1 parent bd395d2 commit 74d53b5

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
@@ -179,14 +179,6 @@ impl<W> From<IntoInnerError<W>> for Error {
179179
}
180180
}
181181

182-
#[stable(feature = "rust1", since = "1.0.0")]
183-
impl<W: Send + fmt::Debug> error::Error for IntoInnerError<W> {
184-
#[allow(deprecated, deprecated_in_future)]
185-
fn description(&self) -> &str {
186-
error::Error::description(self.error())
187-
}
188-
}
189-
190182
#[stable(feature = "rust1", since = "1.0.0")]
191183
impl<W> fmt::Display for IntoInnerError<W> {
192184
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
@@ -293,7 +293,7 @@ pub mod prelude;
293293
mod util;
294294
use crate::{vec::Vec, string::String};
295295

296-
const DEFAULT_BUF_SIZE: usize = crate::sys_common::io::DEFAULT_BUF_SIZE;
296+
const DEFAULT_BUF_SIZE: usize = 8 * 1024;
297297

298298
struct Guard<'a> {
299299
buf: &'a mut Vec<u8>,
@@ -1003,7 +1003,10 @@ pub fn read_to_string<R: Read>(reader: &mut R) -> Result<String> {
10031003
/// Windows.
10041004
#[stable(feature = "iovec", since = "1.36.0")]
10051005
#[repr(transparent)]
1006-
pub struct IoSliceMut<'a>(sys::io::IoSliceMut<'a>);
1006+
pub struct IoSliceMut<'a> {
1007+
vec: iovec,
1008+
_p: PhantomData<&'a [u8]>,
1009+
}
10071010

10081011
#[stable(feature = "iovec-send-sync", since = "1.44.0")]
10091012
unsafe impl<'a> Send for IoSliceMut<'a> {}
@@ -1014,7 +1017,7 @@ unsafe impl<'a> Sync for IoSliceMut<'a> {}
10141017
#[stable(feature = "iovec", since = "1.36.0")]
10151018
impl<'a> fmt::Debug for IoSliceMut<'a> {
10161019
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1017-
fmt::Debug::fmt(self.0.as_slice(), fmt)
1020+
fmt::Debug::fmt(self.as_slice(), fmt)
10181021
}
10191022
}
10201023

@@ -1027,7 +1030,13 @@ impl<'a> IoSliceMut<'a> {
10271030
#[stable(feature = "iovec", since = "1.36.0")]
10281031
#[inline]
10291032
pub fn new(buf: &'a mut [u8]) -> IoSliceMut<'a> {
1030-
IoSliceMut(sys::io::IoSliceMut::new(buf))
1033+
IoSliceMut {
1034+
vec: iovec {
1035+
iov_base: buf.as_mut_ptr() as *mut c_void,
1036+
iov_len: buf.len() as iov_len_t,
1037+
},
1038+
_p: PhantomData,
1039+
}
10311040
}
10321041

10331042
/// Advance the internal cursor of the slice.
@@ -1080,10 +1089,34 @@ impl<'a> IoSliceMut<'a> {
10801089

10811090
let bufs = &mut bufs[remove..];
10821091
if !bufs.is_empty() {
1083-
bufs[0].0.advance(n - accumulated_len)
1092+
bufs[0].advance_self(n - accumulated_len)
10841093
}
10851094
bufs
10861095
}
1096+
1097+
#[inline]
1098+
fn advance_self(&mut self, n: usize) {
1099+
if (self.vec.iov_len as usize) < n {
1100+
panic!("advancing IoSliceMut beyond its length");
1101+
}
1102+
1103+
unsafe {
1104+
self.vec.iov_len -= n as iov_len_t;
1105+
self.vec.iov_base = self.vec.iov_base.add(n);
1106+
}
1107+
}
1108+
1109+
#[inline]
1110+
fn as_slice(&self) -> &[u8] {
1111+
unsafe { slice::from_raw_parts(self.vec.iov_base as *mut u8, self.vec.iov_len as usize) }
1112+
}
1113+
1114+
#[inline]
1115+
fn as_mut_slice(&mut self) -> &mut [u8] {
1116+
unsafe {
1117+
slice::from_raw_parts_mut(self.vec.iov_base as *mut u8, self.vec.iov_len as usize)
1118+
}
1119+
}
10871120
}
10881121

10891122
#[stable(feature = "iovec", since = "1.36.0")]
@@ -1092,15 +1125,15 @@ impl<'a> Deref for IoSliceMut<'a> {
10921125

10931126
#[inline]
10941127
fn deref(&self) -> &[u8] {
1095-
self.0.as_slice()
1128+
self.as_slice()
10961129
}
10971130
}
10981131

10991132
#[stable(feature = "iovec", since = "1.36.0")]
11001133
impl<'a> DerefMut for IoSliceMut<'a> {
11011134
#[inline]
11021135
fn deref_mut(&mut self) -> &mut [u8] {
1103-
self.0.as_mut_slice()
1136+
self.as_mut_slice()
11041137
}
11051138
}
11061139

@@ -1112,7 +1145,10 @@ impl<'a> DerefMut for IoSliceMut<'a> {
11121145
#[stable(feature = "iovec", since = "1.36.0")]
11131146
#[derive(Copy, Clone)]
11141147
#[repr(transparent)]
1115-
pub struct IoSlice<'a>(sys::io::IoSlice<'a>);
1148+
pub struct IoSlice<'a> {
1149+
vec: iovec,
1150+
_p: PhantomData<&'a [u8]>,
1151+
}
11161152

11171153
#[stable(feature = "iovec-send-sync", since = "1.44.0")]
11181154
unsafe impl<'a> Send for IoSlice<'a> {}
@@ -1123,7 +1159,7 @@ unsafe impl<'a> Sync for IoSlice<'a> {}
11231159
#[stable(feature = "iovec", since = "1.36.0")]
11241160
impl<'a> fmt::Debug for IoSlice<'a> {
11251161
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1126-
fmt::Debug::fmt(self.0.as_slice(), fmt)
1162+
fmt::Debug::fmt(self.as_slice(), fmt)
11271163
}
11281164
}
11291165

@@ -1136,7 +1172,13 @@ impl<'a> IoSlice<'a> {
11361172
#[stable(feature = "iovec", since = "1.36.0")]
11371173
#[inline]
11381174
pub fn new(buf: &'a [u8]) -> IoSlice<'a> {
1139-
IoSlice(sys::io::IoSlice::new(buf))
1175+
IoSlice {
1176+
vec: iovec {
1177+
iov_base: buf.as_ptr() as *mut u8 as *mut c_void,
1178+
iov_len: buf.len() as iov_len_t,
1179+
},
1180+
_p: PhantomData,
1181+
}
11401182
}
11411183

11421184
/// Advance the internal cursor of the slice.
@@ -1188,10 +1230,27 @@ impl<'a> IoSlice<'a> {
11881230

11891231
let bufs = &mut bufs[remove..];
11901232
if !bufs.is_empty() {
1191-
bufs[0].0.advance(n - accumulated_len)
1233+
bufs[0].advance_self(n - accumulated_len)
11921234
}
11931235
bufs
11941236
}
1237+
1238+
#[inline]
1239+
fn advance_self(&mut self, n: usize) {
1240+
if (self.vec.iov_len as usize) < n {
1241+
panic!("advancing IoSlice beyond its length");
1242+
}
1243+
1244+
unsafe {
1245+
self.vec.iov_len -= n as iov_len_t;
1246+
self.vec.iov_base = self.vec.iov_base.add(n);
1247+
}
1248+
}
1249+
1250+
#[inline]
1251+
fn as_slice(&self) -> &[u8] {
1252+
unsafe { slice::from_raw_parts(self.vec.iov_base as *mut u8, self.vec.iov_len as usize) }
1253+
}
11951254
}
11961255

11971256
#[stable(feature = "iovec", since = "1.36.0")]
@@ -1200,7 +1259,7 @@ impl<'a> Deref for IoSlice<'a> {
12001259

12011260
#[inline]
12021261
fn deref(&self) -> &[u8] {
1203-
self.0.as_slice()
1262+
self.as_slice()
12041263
}
12051264
}
12061265

0 commit comments

Comments
 (0)