From 4c39b3428e05b8a757dad4ec57f5d95378eb6e2b Mon Sep 17 00:00:00 2001 From: Yonggang Luo Date: Thu, 15 Oct 2020 00:20:01 +0800 Subject: [PATCH 1/9] expose utf8_char_width in str --- library/core/src/str/mod.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/library/core/src/str/mod.rs b/library/core/src/str/mod.rs index 6a35378ca7b50..80953aa8479bc 100644 --- a/library/core/src/str/mod.rs +++ b/library/core/src/str/mod.rs @@ -71,6 +71,9 @@ use iter::SplitInclusive; #[unstable(feature = "str_internals", issue = "none")] pub use validations::next_code_point; +#[unstable(feature = "str_internals", issue = "none")] +pub use validations::utf8_char_width; + use iter::MatchIndicesInternal; use iter::SplitInternal; use iter::{MatchesInternal, SplitNInternal}; From 48fd7d783263ea5221c5c803455c38ad1949aec5 Mon Sep 17 00:00:00 2001 From: Yonggang Luo Date: Tue, 13 Oct 2020 20:24:57 +0000 Subject: [PATCH 2/9] Add ctypes Fixes it first. Add versions for ctypes.rs Improve ctypes.rs --- library/core/src/ctypes.rs | 309 +++++++++++++++++++++++++++++++++++++ library/core/src/lib.rs | 4 + 2 files changed, 313 insertions(+) create mode 100644 library/core/src/ctypes.rs diff --git a/library/core/src/ctypes.rs b/library/core/src/ctypes.rs new file mode 100644 index 0000000000000..dfc1839fa6ba2 --- /dev/null +++ b/library/core/src/ctypes.rs @@ -0,0 +1,309 @@ +//! Type aliases to C types like c_int for use with bindgen +//! +//! # MSRV +//! +//! This crate is guaranteed to compile on stable Rust 1.30.0 and up. It *might* compile with older +//! versions but that may change in any new patch release. + +#![allow(non_camel_case_types)] +#![deny(warnings)] + +// AD = Architecture dependent +#[stable(feature = "core_primitive", since = "1.43.0")] +pub use ad::*; +// OD = OS dependent +#[stable(feature = "core_primitive", since = "1.43.0")] +pub use od::*; +// OD IOVEC = OS dependent iovec +#[stable(feature = "core_primitive", since = "1.43.0")] +pub use od_iovec::*; +// OD wchar = OS dependent wchar_t +#[stable(feature = "core_primitive", since = "1.43.0")] +pub use od_wchar::*; +// OD wint = OS dependent wint_t +#[stable(feature = "core_primitive", since = "1.43.0")] +pub use od_wint::*; +// PWD = Pointer Width Dependent +#[stable(feature = "core_primitive", since = "1.43.0")] +pub use pwd::*; + +//=================================================================== +// c_char, c_int, c_uint +//=================================================================== + +#[cfg(any(target_arch = "aarch64", + target_arch = "arm", + target_arch = "asmjs", + target_arch = "wasm32", + target_arch = "wasm64", + target_arch = "powerpc", + target_arch = "powerpc64", + target_arch = "s390x", + target_arch = "riscv32", + target_arch = "riscv64"))] +mod ad { + use super::c_uchar; + #[stable(feature = "rust1", since = "1.0.0")] + pub type c_char = c_uchar; + + #[stable(feature = "rust1", since = "1.0.0")] + pub type c_int = i32; + #[stable(feature = "rust1", since = "1.0.0")] + pub type c_uint = u32; +} + +#[cfg(any(target_arch = "mips", + target_arch = "mips64", + target_arch = "sparc64", + target_arch = "x86", + target_arch = "x86_64", + target_arch = "nvptx", + target_arch = "nvptx64", + target_arch = "xtensa"))] +mod ad { + use super::c_schar; + #[stable(feature = "rust1", since = "1.0.0")] + pub type c_char = c_schar; + + #[stable(feature = "rust1", since = "1.0.0")] + pub type c_int = i32; + #[stable(feature = "rust1", since = "1.0.0")] + pub type c_uint = u32; +} + +#[cfg(target_arch = "msp430")] +mod ad { + use super::c_uchar; + #[stable(feature = "rust1", since = "1.0.0")] + pub type c_char = c_uchar; + + #[stable(feature = "rust1", since = "1.0.0")] + pub type c_int = i16; + #[stable(feature = "rust1", since = "1.0.0")] + pub type c_uint = u16; +} + +//=================================================================== +// c_long, c_ulong +//=================================================================== + +// NOTE c_{,u}long definitions come from libc v0.2.3 +#[cfg(not( + any(target_os = "windows", + target_os = "redox", + target_os = "solaris")))] +mod od { +#[cfg(any(target_pointer_width = "16", + target_pointer_width = "32"))] +#[stable(feature = "rust1", since = "1.0.0")] +pub type c_long = i32; +#[cfg(any(target_pointer_width = "16", + target_pointer_width = "32"))] +#[stable(feature = "rust1", since = "1.0.0")] +pub type c_ulong = u32; + +#[cfg(target_pointer_width = "64")] +#[stable(feature = "rust1", since = "1.0.0")] +pub type c_long = i64; +#[cfg(target_pointer_width = "64")] +#[stable(feature = "rust1", since = "1.0.0")] +pub type c_ulong = u64; +} + +#[cfg(any(target_os = "windows"))] +#[stable(feature = "rust1", since = "1.0.0")] +mod od { +#[stable(feature = "rust1", since = "1.0.0")] +pub type c_long = i32; +#[stable(feature = "rust1", since = "1.0.0")] +pub type c_ulong = u32; +} + +#[cfg(any(target_os = "redox", + target_os = "solaris"))] +#[stable(feature = "rust1", since = "1.0.0")] +mod od { +#[stable(feature = "rust1", since = "1.0.0")] +pub type c_long = i64; +#[stable(feature = "rust1", since = "1.0.0")] +pub type c_ulong = u64; +} + +//=================================================================== +// int8_t to c_void, all platform are the same +//=================================================================== + +#[stable(feature = "rust1", since = "1.0.0")] +pub type int8_t = i8; +#[stable(feature = "rust1", since = "1.0.0")] +pub type int16_t = i16; +#[stable(feature = "rust1", since = "1.0.0")] +pub type int32_t = i32; +#[stable(feature = "rust1", since = "1.0.0")] +pub type int64_t = i64; + +#[stable(feature = "rust1", since = "1.0.0")] +pub type uint8_t = u8; +#[stable(feature = "rust1", since = "1.0.0")] +pub type uint16_t = u16; +#[stable(feature = "rust1", since = "1.0.0")] +pub type uint32_t = u32; +#[stable(feature = "rust1", since = "1.0.0")] +pub type uint64_t = u64; + +#[stable(feature = "rust1", since = "1.0.0")] +pub type c_schar = i8; +#[stable(feature = "rust1", since = "1.0.0")] +pub type c_short = i16; +#[stable(feature = "rust1", since = "1.0.0")] +pub type c_longlong = i64; + +#[stable(feature = "rust1", since = "1.0.0")] +pub type c_uchar = u8; +#[stable(feature = "rust1", since = "1.0.0")] +pub type c_ushort = u16; +#[stable(feature = "rust1", since = "1.0.0")] +pub type c_ulonglong = u64; + +#[stable(feature = "rust1", since = "1.0.0")] +pub type c_float = f32; +#[stable(feature = "rust1", since = "1.0.0")] +pub type c_double = f64; + +#[stable(feature = "rust1", since = "1.0.0")] +pub type intmax_t = i64; +#[stable(feature = "rust1", since = "1.0.0")] +pub type uintmax_t = u64; + +#[stable(feature = "rust1", since = "1.0.0")] +pub type size_t = usize; +#[stable(feature = "rust1", since = "1.0.0")] +pub type ptrdiff_t = isize; +#[stable(feature = "rust1", since = "1.0.0")] +pub type intptr_t = isize; +#[stable(feature = "rust1", since = "1.0.0")] +pub type uintptr_t = usize; +#[stable(feature = "rust1", since = "1.0.0")] +pub type ssize_t = isize; + +#[stable(feature = "core_primitive", since = "1.43.0")] +pub type va_list<'a,'b> = super::ffi::VaList<'a, 'b>; + +#[stable(feature = "rust1", since = "1.0.0")] +pub type c_void = crate::ffi::c_void; + +//=================================================================== +// struct iovec, iov_len_t +//=================================================================== + +// iovec is special to move part of std::io component into core, other +// struct type doesn't have such limitation. +// Add iov_len_t for easily do cast, becase windows are different + +#[cfg(not(any( + target_os = "windows")))] +mod od_iovec { +use super::size_t; +#[stable(feature = "rust1", since = "1.0.0")] +use super::c_void; +#[stable(feature = "rust1", since = "1.0.0")] +pub type iov_len_t = size_t; +/// Refer to https://pubs.opengroup.org/onlinepubs/009695399/basedefs/sys/uio.h.html +/// Refer to https://github.com/bminor/glibc/blob/master/misc/bits/types/struct_iovec.h +#[repr(C)] +#[derive(Copy, Clone,Debug)] +#[stable(feature = "rust1", since = "1.0.0")] +pub struct iovec { + #[stable(feature = "rust1", since = "1.0.0")] + pub iov_base: *mut c_void, + #[stable(feature = "rust1", since = "1.0.0")] + pub iov_len: iov_len_t, +} +} + +#[cfg(any(target_os = "windows"))] +mod od_iovec { +use super::c_ulong; +use super::c_void; +/// Refer to https://docs.microsoft.com/en-us/windows/win32/api/ws2def/ns-ws2def-wsabuf +#[stable(feature = "rust1", since = "1.0.0")] +pub type iov_len_t = c_ulong; +#[repr(C)] +#[derive(Copy, Clone,Debug)] +#[stable(feature = "rust1", since = "1.0.0")] +pub struct iovec { + #[stable(feature = "rust1", since = "1.0.0")] + pub iov_len: iov_len_t, + #[stable(feature = "rust1", since = "1.0.0")] + pub iov_base: *mut c_void, +} +} + +//=================================================================== +// wchar_t +//=================================================================== +#[cfg(not( + any(target_os = "windows", + target_os = "vxworks")))] +mod od_wchar { +use super::int32_t; +#[stable(feature = "rust1", since = "1.0.0")] +pub type wchar_t = int32_t; +} + +#[cfg(any(target_os = "windows", + target_os = "vxworks"))] +mod od_wchar { +use super::uint16_t; +#[stable(feature = "rust1", since = "1.0.0")] +pub type wchar_t = uint16_t; +} + +//=================================================================== +// wint_t +//=================================================================== +#[cfg(not( + any(target_os = "windows", + target_os = "vxworks")))] +mod od_wint { +use super::uint32_t; +#[stable(feature = "rust1", since = "1.0.0")] +pub type wint_t = uint32_t; +#[stable(feature = "rust1", since = "1.0.0")] +pub const WEOF: wint_t = 0xFFFF_FFFFu32; +} + +#[cfg(any(target_os = "windows"))] +mod od_wint { +use super::uint16_t; +#[stable(feature = "rust1", since = "1.0.0")] +pub type wint_t = uint16_t; +#[stable(feature = "rust1", since = "1.0.0")] +pub const WEOF: wint_t = 0xFFFFu16; +} + +#[cfg(any(target_os = "vxworks"))] +mod od_wint { +use super::int32_t; +#[stable(feature = "rust1", since = "1.0.0")] +pub type wint_t = int32_t; +#[stable(feature = "rust1", since = "1.0.0")] +pub const WEOF: wint_t = -1i32; +} + +//=================================================================== +// wctype_t TODO: This is os dependent +//=================================================================== +#[stable(feature = "rust1", since = "1.0.0")] +pub type wctype_t = i64; + +//=================================================================== +// pwd not used yet +//=================================================================== + +#[cfg(target_pointer_width = "32")] +mod pwd {} + +#[cfg(target_pointer_width = "64")] +mod pwd {} + diff --git a/library/core/src/lib.rs b/library/core/src/lib.rs index fd4a76c1eb548..6cf692acbfa77 100644 --- a/library/core/src/lib.rs +++ b/library/core/src/lib.rs @@ -270,6 +270,10 @@ pub mod task; #[allow(missing_docs)] pub mod alloc; +#[stable(feature = "core_primitive", since = "1.43.0")] +#[allow(missing_docs)] +pub mod ctypes; + // note: does not need to be public mod bool; mod tuple; From c13b5ce84876e9f8e2712d03d7bfd3dd1488ba17 Mon Sep 17 00:00:00 2001 From: Yonggang Luo Date: Fri, 16 Oct 2020 14:14:39 +0800 Subject: [PATCH 3/9] add std io --- library/{std => alloc}/src/io/buffered/bufreader.rs | 0 library/{std => alloc}/src/io/buffered/bufwriter.rs | 0 library/{std => alloc}/src/io/buffered/linewriter.rs | 0 library/{std => alloc}/src/io/buffered/linewritershim.rs | 0 library/{std => alloc}/src/io/buffered/mod.rs | 0 library/{std => alloc}/src/io/buffered/tests.rs | 0 library/{std => alloc}/src/io/cursor.rs | 0 library/{std => alloc}/src/io/cursor/tests.rs | 0 library/{std => alloc}/src/io/error.rs | 0 library/{std => alloc}/src/io/error/tests.rs | 0 library/{std => alloc}/src/io/impls.rs | 0 library/{std => alloc}/src/io/impls/tests.rs | 0 library/{std => alloc}/src/io/mod.rs | 0 library/{std => alloc}/src/io/prelude.rs | 0 library/{std => alloc}/src/io/stdio.rs | 0 library/{std => alloc}/src/io/stdio/tests.rs | 0 library/{std => alloc}/src/io/tests.rs | 0 library/{std => alloc}/src/io/util.rs | 0 library/{std => alloc}/src/io/util/tests.rs | 0 19 files changed, 0 insertions(+), 0 deletions(-) rename library/{std => alloc}/src/io/buffered/bufreader.rs (100%) rename library/{std => alloc}/src/io/buffered/bufwriter.rs (100%) rename library/{std => alloc}/src/io/buffered/linewriter.rs (100%) rename library/{std => alloc}/src/io/buffered/linewritershim.rs (100%) rename library/{std => alloc}/src/io/buffered/mod.rs (100%) rename library/{std => alloc}/src/io/buffered/tests.rs (100%) rename library/{std => alloc}/src/io/cursor.rs (100%) rename library/{std => alloc}/src/io/cursor/tests.rs (100%) rename library/{std => alloc}/src/io/error.rs (100%) rename library/{std => alloc}/src/io/error/tests.rs (100%) rename library/{std => alloc}/src/io/impls.rs (100%) rename library/{std => alloc}/src/io/impls/tests.rs (100%) rename library/{std => alloc}/src/io/mod.rs (100%) rename library/{std => alloc}/src/io/prelude.rs (100%) rename library/{std => alloc}/src/io/stdio.rs (100%) rename library/{std => alloc}/src/io/stdio/tests.rs (100%) rename library/{std => alloc}/src/io/tests.rs (100%) rename library/{std => alloc}/src/io/util.rs (100%) rename library/{std => alloc}/src/io/util/tests.rs (100%) diff --git a/library/std/src/io/buffered/bufreader.rs b/library/alloc/src/io/buffered/bufreader.rs similarity index 100% rename from library/std/src/io/buffered/bufreader.rs rename to library/alloc/src/io/buffered/bufreader.rs diff --git a/library/std/src/io/buffered/bufwriter.rs b/library/alloc/src/io/buffered/bufwriter.rs similarity index 100% rename from library/std/src/io/buffered/bufwriter.rs rename to library/alloc/src/io/buffered/bufwriter.rs diff --git a/library/std/src/io/buffered/linewriter.rs b/library/alloc/src/io/buffered/linewriter.rs similarity index 100% rename from library/std/src/io/buffered/linewriter.rs rename to library/alloc/src/io/buffered/linewriter.rs diff --git a/library/std/src/io/buffered/linewritershim.rs b/library/alloc/src/io/buffered/linewritershim.rs similarity index 100% rename from library/std/src/io/buffered/linewritershim.rs rename to library/alloc/src/io/buffered/linewritershim.rs diff --git a/library/std/src/io/buffered/mod.rs b/library/alloc/src/io/buffered/mod.rs similarity index 100% rename from library/std/src/io/buffered/mod.rs rename to library/alloc/src/io/buffered/mod.rs diff --git a/library/std/src/io/buffered/tests.rs b/library/alloc/src/io/buffered/tests.rs similarity index 100% rename from library/std/src/io/buffered/tests.rs rename to library/alloc/src/io/buffered/tests.rs diff --git a/library/std/src/io/cursor.rs b/library/alloc/src/io/cursor.rs similarity index 100% rename from library/std/src/io/cursor.rs rename to library/alloc/src/io/cursor.rs diff --git a/library/std/src/io/cursor/tests.rs b/library/alloc/src/io/cursor/tests.rs similarity index 100% rename from library/std/src/io/cursor/tests.rs rename to library/alloc/src/io/cursor/tests.rs diff --git a/library/std/src/io/error.rs b/library/alloc/src/io/error.rs similarity index 100% rename from library/std/src/io/error.rs rename to library/alloc/src/io/error.rs diff --git a/library/std/src/io/error/tests.rs b/library/alloc/src/io/error/tests.rs similarity index 100% rename from library/std/src/io/error/tests.rs rename to library/alloc/src/io/error/tests.rs diff --git a/library/std/src/io/impls.rs b/library/alloc/src/io/impls.rs similarity index 100% rename from library/std/src/io/impls.rs rename to library/alloc/src/io/impls.rs diff --git a/library/std/src/io/impls/tests.rs b/library/alloc/src/io/impls/tests.rs similarity index 100% rename from library/std/src/io/impls/tests.rs rename to library/alloc/src/io/impls/tests.rs diff --git a/library/std/src/io/mod.rs b/library/alloc/src/io/mod.rs similarity index 100% rename from library/std/src/io/mod.rs rename to library/alloc/src/io/mod.rs diff --git a/library/std/src/io/prelude.rs b/library/alloc/src/io/prelude.rs similarity index 100% rename from library/std/src/io/prelude.rs rename to library/alloc/src/io/prelude.rs diff --git a/library/std/src/io/stdio.rs b/library/alloc/src/io/stdio.rs similarity index 100% rename from library/std/src/io/stdio.rs rename to library/alloc/src/io/stdio.rs diff --git a/library/std/src/io/stdio/tests.rs b/library/alloc/src/io/stdio/tests.rs similarity index 100% rename from library/std/src/io/stdio/tests.rs rename to library/alloc/src/io/stdio/tests.rs diff --git a/library/std/src/io/tests.rs b/library/alloc/src/io/tests.rs similarity index 100% rename from library/std/src/io/tests.rs rename to library/alloc/src/io/tests.rs diff --git a/library/std/src/io/util.rs b/library/alloc/src/io/util.rs similarity index 100% rename from library/std/src/io/util.rs rename to library/alloc/src/io/util.rs diff --git a/library/std/src/io/util/tests.rs b/library/alloc/src/io/util/tests.rs similarity index 100% rename from library/std/src/io/util/tests.rs rename to library/alloc/src/io/util/tests.rs From 374c7aea92967b3b6a9b531588e03af9c85a7766 Mon Sep 17 00:00:00 2001 From: Yonggang Luo Date: Fri, 16 Oct 2020 13:01:57 +0800 Subject: [PATCH 4/9] unsafe things. --- library/alloc/src/io/buffered/bufreader.rs | 2 +- library/alloc/src/io/cursor.rs | 2 +- library/alloc/src/io/impls.rs | 6 +++--- library/alloc/src/io/mod.rs | 10 +++++++--- library/alloc/src/io/util.rs | 4 ++-- 5 files changed, 14 insertions(+), 10 deletions(-) diff --git a/library/alloc/src/io/buffered/bufreader.rs b/library/alloc/src/io/buffered/bufreader.rs index 987371f50ec22..823cdcc49fc80 100644 --- a/library/alloc/src/io/buffered/bufreader.rs +++ b/library/alloc/src/io/buffered/bufreader.rs @@ -305,7 +305,7 @@ impl Read for BufReader { // we can't skip unconditionally because of the large buffer case in read. unsafe fn initializer(&self) -> Initializer { - self.inner.initializer() + unsafe { (*self).initializer() } } } diff --git a/library/alloc/src/io/cursor.rs b/library/alloc/src/io/cursor.rs index bbee2cc98425e..d74ff1705d9e6 100644 --- a/library/alloc/src/io/cursor.rs +++ b/library/alloc/src/io/cursor.rs @@ -281,7 +281,7 @@ where #[inline] unsafe fn initializer(&self) -> Initializer { - Initializer::nop() + unsafe { Initializer::nop() } } } diff --git a/library/alloc/src/io/impls.rs b/library/alloc/src/io/impls.rs index 00bf8b9af7384..97799fd9e2d24 100644 --- a/library/alloc/src/io/impls.rs +++ b/library/alloc/src/io/impls.rs @@ -30,7 +30,7 @@ impl Read for &mut R { #[inline] unsafe fn initializer(&self) -> Initializer { - (**self).initializer() + unsafe { (**self).initializer() } } #[inline] @@ -129,7 +129,7 @@ impl Read for Box { #[inline] unsafe fn initializer(&self) -> Initializer { - (**self).initializer() + unsafe { (**self).initializer() } } #[inline] @@ -256,7 +256,7 @@ impl Read for &[u8] { #[inline] unsafe fn initializer(&self) -> Initializer { - Initializer::nop() + unsafe { Initializer::nop() } } #[inline] diff --git a/library/alloc/src/io/mod.rs b/library/alloc/src/io/mod.rs index db3b0e2628f2a..dab758255e521 100644 --- a/library/alloc/src/io/mod.rs +++ b/library/alloc/src/io/mod.rs @@ -2215,8 +2215,10 @@ impl Read for Chain { } unsafe fn initializer(&self) -> Initializer { - let initializer = self.first.initializer(); - if initializer.should_initialize() { initializer } else { self.second.initializer() } + unsafe { + let initializer = self.first.initializer(); + if initializer.should_initialize() { initializer } else { self.second.initializer() } + } } } @@ -2406,7 +2408,9 @@ impl Read for Take { } unsafe fn initializer(&self) -> Initializer { - self.inner.initializer() + unsafe { + self.inner.initializer() + } } fn read_to_end(&mut self, buf: &mut Vec) -> Result { diff --git a/library/alloc/src/io/util.rs b/library/alloc/src/io/util.rs index e43ce4cdb4b8e..a8cc5039a13e5 100644 --- a/library/alloc/src/io/util.rs +++ b/library/alloc/src/io/util.rs @@ -45,7 +45,7 @@ impl Read for Empty { #[inline] unsafe fn initializer(&self) -> Initializer { - Initializer::nop() + unsafe { Initializer::nop() } } } #[stable(feature = "rust1", since = "1.0.0")] @@ -135,7 +135,7 @@ impl Read for Repeat { #[inline] unsafe fn initializer(&self) -> Initializer { - Initializer::nop() + unsafe { Initializer::nop() } } } From e7d3f248e930b655ccbc4c673d05809a6a58b15a Mon Sep 17 00:00:00 2001 From: Yonggang Luo Date: Fri, 16 Oct 2020 15:20:09 +0800 Subject: [PATCH 5/9] fixes unsafe --- library/alloc/src/io/buffered/bufreader.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/alloc/src/io/buffered/bufreader.rs b/library/alloc/src/io/buffered/bufreader.rs index 823cdcc49fc80..4102f3427fb70 100644 --- a/library/alloc/src/io/buffered/bufreader.rs +++ b/library/alloc/src/io/buffered/bufreader.rs @@ -305,7 +305,7 @@ impl Read for BufReader { // we can't skip unconditionally because of the large buffer case in read. unsafe fn initializer(&self) -> Initializer { - unsafe { (*self).initializer() } + unsafe { self.inner.initializer() } } } From 662ac255760ffb9f761c8d7599c67b62592d818a Mon Sep 17 00:00:00 2001 From: Yonggang Luo Date: Thu, 15 Oct 2020 00:16:55 +0800 Subject: [PATCH 6/9] Expose buffer for LineWriter --- library/alloc/src/io/buffered/linewriter.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/library/alloc/src/io/buffered/linewriter.rs b/library/alloc/src/io/buffered/linewriter.rs index 502c6e3c6c0b9..759a3372ba01d 100644 --- a/library/alloc/src/io/buffered/linewriter.rs +++ b/library/alloc/src/io/buffered/linewriter.rs @@ -155,6 +155,24 @@ impl LineWriter { self.inner.get_mut() } + /// Returns a reference to the internally buffered data. + /// + /// # Examples + /// + /// ```no_run + /// use std::io::LineWriter; + /// use std::net::TcpStream; + /// + /// let buf_writer = LineWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap()); + /// + /// // See how many bytes are currently buffered + /// let bytes_buffered = buf_writer.buffer().len(); + /// ``` + #[stable(feature = "rust1", since = "1.0.0")] + pub fn buffer(&self) -> &[u8] { + &self.inner.buffer() + } + /// Unwraps this `LineWriter`, returning the underlying writer. /// /// The internal buffer is written out before returning the writer. From 58150ac95273b10a895f1412d97366de48c3d161 Mon Sep 17 00:00:00 2001 From: Yonggang Luo Date: Fri, 16 Oct 2020 13:17:03 +0800 Subject: [PATCH 7/9] remove Cargo.toml --- Cargo.toml | 110 ----------------------------------------------------- 1 file changed, 110 deletions(-) delete mode 100644 Cargo.toml diff --git a/Cargo.toml b/Cargo.toml deleted file mode 100644 index f961d3e9b97be..0000000000000 --- a/Cargo.toml +++ /dev/null @@ -1,110 +0,0 @@ -[workspace] -members = [ - "src/bootstrap", - "compiler/rustc", - "library/std", - "library/test", - "src/rustdoc-json-types", - "src/tools/cargotest", - "src/tools/clippy", - "src/tools/compiletest", - "src/tools/error_index_generator", - "src/tools/linkchecker", - "src/tools/lint-docs", - "src/tools/rustbook", - "src/tools/unstable-book-gen", - "src/tools/tidy", - "src/tools/tier-check", - "src/tools/build-manifest", - "src/tools/remote-test-client", - "src/tools/remote-test-server", - "src/tools/rust-installer", - "src/tools/rust-demangler", - "src/tools/cargo", - "src/tools/cargo/crates/credential/cargo-credential-1password", - "src/tools/cargo/crates/credential/cargo-credential-macos-keychain", - "src/tools/cargo/crates/credential/cargo-credential-wincred", - "src/tools/rustdoc", - "src/tools/rls", - "src/tools/rustfmt", - "src/tools/miri", - "src/tools/miri/cargo-miri", - "src/tools/rustdoc-themes", - "src/tools/unicode-table-generator", - "src/tools/expand-yaml-anchors", - "src/tools/jsondocck", -] - -exclude = [ - "build", - "compiler/rustc_codegen_cranelift", - # HACK(eddyb) This hardcodes the fact that our CI uses `/checkout/obj`. - "obj", - # The `x` binary is a thin wrapper that calls `x.py`, which initializes - # submodules, before which workspace members cannot be invoked because - # not all `Cargo.toml` files are available, so we exclude the `x` binary, - # so it can be invoked before the current checkout is set up. - "src/tools/x", -] - -[profile.release.package.compiler_builtins] -# The compiler-builtins crate cannot reference libcore, and it's own CI will -# verify that this is the case. This requires, however, that the crate is built -# without overflow checks and debug assertions. Forcefully disable debug -# assertions and overflow checks here which should ensure that even if these -# assertions are enabled for libstd we won't enable then for compiler_builtins -# which should ensure we still link everything correctly. -debug-assertions = false -overflow-checks = false - -# For compiler-builtins we always use a high number of codegen units. -# The goal here is to place every single intrinsic into its own object -# file to avoid symbol clashes with the system libgcc if possible. Note -# that this number doesn't actually produce this many object files, we -# just don't create more than this number of object files. -# -# It's a bit of a bummer that we have to pass this here, unfortunately. -# Ideally this would be specified through an env var to Cargo so Cargo -# knows how many CGUs are for this specific crate, but for now -# per-crate configuration isn't specifiable in the environment. -codegen-units = 10000 - -# These dependencies of the standard library implement symbolication for -# backtraces on most platforms. Their debuginfo causes both linking to be slower -# (more data to chew through) and binaries to be larger without really all that -# much benefit. This section turns them all to down to have no debuginfo which -# helps to improve link times a little bit. -[profile.release.package] -addr2line.debug = 0 -adler.debug = 0 -gimli.debug = 0 -miniz_oxide.debug = 0 -object.debug = 0 - -# We want the RLS to use the version of Cargo that we've got vendored in this -# repository to ensure that the same exact version of Cargo is used by both the -# RLS and the Cargo binary itself. The RLS depends on Cargo as a git repository -# so we use a `[patch]` here to override the github repository with our local -# vendored copy. -[patch."https://github.com/rust-lang/cargo"] -cargo = { path = "src/tools/cargo" } - -[patch."https://github.com/rust-lang/rustfmt"] -# Similar to Cargo above we want the RLS to use a vendored version of `rustfmt` -# that we're shipping as well (to ensure that the rustfmt in RLS and the -# `rustfmt` executable are the same exact version). -rustfmt-nightly = { path = "src/tools/rustfmt" } - -[patch.crates-io] -# See comments in `src/tools/rustc-workspace-hack/README.md` for what's going on -# here -rustc-workspace-hack = { path = 'src/tools/rustc-workspace-hack' } - -# See comments in `library/rustc-std-workspace-core/README.md` for what's going on -# here -rustc-std-workspace-core = { path = 'library/rustc-std-workspace-core' } -rustc-std-workspace-alloc = { path = 'library/rustc-std-workspace-alloc' } -rustc-std-workspace-std = { path = 'library/rustc-std-workspace-std' } - -[patch."https://github.com/rust-lang/rust-clippy"] -clippy_lints = { path = "src/tools/clippy/clippy_lints" } From bd395d27bd2185746a32a84efd97dfbc381e63e0 Mon Sep 17 00:00:00 2001 From: Yonggang Luo Date: Wed, 14 Oct 2020 05:46:13 +0800 Subject: [PATCH 8/9] Import fixes. --- library/alloc/src/io/buffered/bufreader.rs | 3 ++- library/alloc/src/io/buffered/bufwriter.rs | 1 + .../alloc/src/io/buffered/linewritershim.rs | 2 +- library/alloc/src/io/buffered/mod.rs | 1 - library/alloc/src/io/cursor.rs | 3 ++- library/alloc/src/io/error.rs | 9 ++++----- library/alloc/src/io/impls.rs | 7 ++++--- library/alloc/src/io/mod.rs | 20 ++++++++++--------- library/alloc/src/io/util.rs | 5 +++-- library/alloc/src/lib.rs | 1 + 10 files changed, 29 insertions(+), 23 deletions(-) diff --git a/library/alloc/src/io/buffered/bufreader.rs b/library/alloc/src/io/buffered/bufreader.rs index 4102f3427fb70..2d85b371432d9 100644 --- a/library/alloc/src/io/buffered/bufreader.rs +++ b/library/alloc/src/io/buffered/bufreader.rs @@ -1,6 +1,7 @@ -use crate::cmp; +use core::cmp; use crate::fmt; use crate::io::{self, BufRead, Initializer, IoSliceMut, Read, Seek, SeekFrom, DEFAULT_BUF_SIZE}; +use crate::{boxed::Box, vec::Vec}; /// The `BufReader` struct adds buffering to any reader. /// diff --git a/library/alloc/src/io/buffered/bufwriter.rs b/library/alloc/src/io/buffered/bufwriter.rs index 65bc2fcf00ae0..5dbba22fb1cd7 100644 --- a/library/alloc/src/io/buffered/bufwriter.rs +++ b/library/alloc/src/io/buffered/bufwriter.rs @@ -4,6 +4,7 @@ use crate::io::{ self, Error, ErrorKind, IntoInnerError, IoSlice, Seek, SeekFrom, Write, DEFAULT_BUF_SIZE, }; use crate::mem; +use crate::vec::Vec; /// Wraps a writer and buffers its output. /// diff --git a/library/alloc/src/io/buffered/linewritershim.rs b/library/alloc/src/io/buffered/linewritershim.rs index d0c859d2e0c87..ff76fd3140d6f 100644 --- a/library/alloc/src/io/buffered/linewritershim.rs +++ b/library/alloc/src/io/buffered/linewritershim.rs @@ -1,5 +1,5 @@ use crate::io::{self, BufWriter, IoSlice, Write}; -use crate::memchr; +use core::slice::memchr; /// Private helper struct for implementing the line-buffered writing logic. /// This shim temporarily wraps a BufWriter, and uses its internals to diff --git a/library/alloc/src/io/buffered/mod.rs b/library/alloc/src/io/buffered/mod.rs index 65497817f8160..7139968648c9b 100644 --- a/library/alloc/src/io/buffered/mod.rs +++ b/library/alloc/src/io/buffered/mod.rs @@ -8,7 +8,6 @@ mod linewritershim; #[cfg(test)] mod tests; -use crate::error; use crate::fmt; use crate::io::Error; diff --git a/library/alloc/src/io/cursor.rs b/library/alloc/src/io/cursor.rs index d74ff1705d9e6..a08ae3bacd366 100644 --- a/library/alloc/src/io/cursor.rs +++ b/library/alloc/src/io/cursor.rs @@ -3,10 +3,11 @@ mod tests; use crate::io::prelude::*; -use crate::cmp; +use core::cmp; use crate::io::{self, Error, ErrorKind, Initializer, IoSlice, IoSliceMut, SeekFrom}; use core::convert::TryInto; +use crate::{vec::Vec, boxed::Box}; /// A `Cursor` wraps an in-memory buffer and provides it with a /// [`Seek`] implementation. diff --git a/library/alloc/src/io/error.rs b/library/alloc/src/io/error.rs index ba0f0a0cd714a..c6cff1d0a71ae 100644 --- a/library/alloc/src/io/error.rs +++ b/library/alloc/src/io/error.rs @@ -1,11 +1,10 @@ #[cfg(test)] mod tests; -use crate::convert::From; -use crate::error; -use crate::fmt; -use crate::result; -use crate::sys; +use core::convert::From; +use core::fmt; +use core::result; +use crate::{boxed::Box, string::String}; /// A specialized [`Result`] type for I/O operations. /// diff --git a/library/alloc/src/io/impls.rs b/library/alloc/src/io/impls.rs index 97799fd9e2d24..ddefa76beab90 100644 --- a/library/alloc/src/io/impls.rs +++ b/library/alloc/src/io/impls.rs @@ -1,12 +1,13 @@ #[cfg(test)] mod tests; -use crate::cmp; -use crate::fmt; +use core::cmp; +use core::fmt; use crate::io::{ self, BufRead, Error, ErrorKind, Initializer, IoSlice, IoSliceMut, Read, Seek, SeekFrom, Write, }; -use crate::mem; +use core::mem; +use crate::{vec::Vec, boxed::Box, string::String}; // ============================================================================= // Forwarding implementations diff --git a/library/alloc/src/io/mod.rs b/library/alloc/src/io/mod.rs index dab758255e521..e696de34d0173 100644 --- a/library/alloc/src/io/mod.rs +++ b/library/alloc/src/io/mod.rs @@ -248,17 +248,19 @@ #![stable(feature = "rust1", since = "1.0.0")] +use core::{marker::PhantomData}; +use core::ctypes::{c_void, iovec, iov_len_t}; + #[cfg(test)] mod tests; -use crate::cmp; -use crate::fmt; -use crate::memchr; -use crate::ops::{Deref, DerefMut}; -use crate::ptr; -use crate::slice; -use crate::str; -use crate::sys; +use core::cmp; +use core::fmt; +use core::slice::memchr; +use core::ops::{Deref, DerefMut}; +use core::ptr; +use core::slice; +use core::str; #[stable(feature = "rust1", since = "1.0.0")] pub use self::buffered::IntoInnerError; @@ -288,8 +290,8 @@ mod cursor; mod error; mod impls; pub mod prelude; -mod stdio; mod util; +use crate::{vec::Vec, string::String}; const DEFAULT_BUF_SIZE: usize = crate::sys_common::io::DEFAULT_BUF_SIZE; diff --git a/library/alloc/src/io/util.rs b/library/alloc/src/io/util.rs index a8cc5039a13e5..c6f6806f4cba2 100644 --- a/library/alloc/src/io/util.rs +++ b/library/alloc/src/io/util.rs @@ -3,8 +3,9 @@ #[cfg(test)] mod tests; -use crate::fmt; -use crate::io::{self, BufRead, Initializer, IoSlice, IoSliceMut, Read, Seek, SeekFrom, Write}; +use core::fmt; +use crate::io::{self, BufRead, ErrorKind, Initializer, IoSlice, IoSliceMut, Read, Seek, SeekFrom, Write}; +use core::mem::MaybeUninit; /// A reader which is always at EOF. /// diff --git a/library/alloc/src/lib.rs b/library/alloc/src/lib.rs index 99c42a4ba4423..c060f9d4e9a30 100644 --- a/library/alloc/src/lib.rs +++ b/library/alloc/src/lib.rs @@ -184,6 +184,7 @@ pub mod task; #[cfg(test)] mod tests; pub mod vec; +pub mod io; #[doc(hidden)] #[unstable(feature = "liballoc_internals", issue = "none", reason = "implementation detail")] From 74d53b5e0992b2aeddb80c6f6520f1b9362bc50a Mon Sep 17 00:00:00 2001 From: Yonggang Luo Date: Fri, 16 Oct 2020 13:40:45 +0800 Subject: [PATCH 9/9] Fixes alloc::io by removing use String instead complex Error parameter. --- library/alloc/src/io/buffered/mod.rs | 8 --- library/alloc/src/io/error.rs | 76 ++++++------------------- library/alloc/src/io/mod.rs | 83 ++++++++++++++++++++++++---- 3 files changed, 88 insertions(+), 79 deletions(-) diff --git a/library/alloc/src/io/buffered/mod.rs b/library/alloc/src/io/buffered/mod.rs index 7139968648c9b..22fa92d7a1f53 100644 --- a/library/alloc/src/io/buffered/mod.rs +++ b/library/alloc/src/io/buffered/mod.rs @@ -179,14 +179,6 @@ impl From> for Error { } } -#[stable(feature = "rust1", since = "1.0.0")] -impl error::Error for IntoInnerError { - #[allow(deprecated, deprecated_in_future)] - fn description(&self) -> &str { - error::Error::description(self.error()) - } -} - #[stable(feature = "rust1", since = "1.0.0")] impl fmt::Display for IntoInnerError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { diff --git a/library/alloc/src/io/error.rs b/library/alloc/src/io/error.rs index c6cff1d0a71ae..4f39afa765ff9 100644 --- a/library/alloc/src/io/error.rs +++ b/library/alloc/src/io/error.rs @@ -74,7 +74,7 @@ enum Repr { #[derive(Debug)] struct Custom { kind: ErrorKind, - error: Box, + error: String, } /// A list specifying general categories of I/O error. @@ -177,6 +177,12 @@ pub enum ErrorKind { /// read. #[stable(feature = "read_exact", since = "1.6.0")] UnexpectedEof, + + /// A marker variant that tells the compiler that users of this enum cannot + /// match it exhaustively. + #[doc(hidden)] + #[stable(feature = "read_exact", since = "1.6.0")] + __Nonexhaustive, } impl ErrorKind { @@ -200,6 +206,7 @@ impl ErrorKind { ErrorKind::Interrupted => "operation interrupted", ErrorKind::Other => "other os error", ErrorKind::UnexpectedEof => "unexpected end of file", + _ => "unknown error", } } } @@ -249,33 +256,15 @@ impl Error { #[stable(feature = "rust1", since = "1.0.0")] pub fn new(kind: ErrorKind, error: E) -> Error where - E: Into>, + E: Into, { Self::_new(kind, error.into()) } - fn _new(kind: ErrorKind, error: Box) -> Error { + fn _new(kind: ErrorKind, error: String) -> Error { Error { repr: Repr::Custom(Box::new(Custom { kind, error })) } } - /// Returns an error representing the last OS error which occurred. - /// - /// This function reads the value of `errno` for the target platform (e.g. - /// `GetLastError` on Windows) and will return a corresponding instance of - /// [`Error`] for the error code. - /// - /// # Examples - /// - /// ``` - /// use std::io::Error; - /// - /// println!("last OS error: {:?}", Error::last_os_error()); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - pub fn last_os_error() -> Error { - Error::from_raw_os_error(sys::os::errno() as i32) - } - /// Creates a new instance of an [`Error`] from a particular OS error code. /// /// # Examples @@ -372,11 +361,11 @@ impl Error { /// } /// ``` #[stable(feature = "io_error_inner", since = "1.3.0")] - pub fn get_ref(&self) -> Option<&(dyn error::Error + Send + Sync + 'static)> { + pub fn get_ref(&self) -> Option<&String> { match self.repr { Repr::Os(..) => None, Repr::Simple(..) => None, - Repr::Custom(ref c) => Some(&*c.error), + Repr::Custom(ref c) => Some(&c.error), } } @@ -443,11 +432,11 @@ impl Error { /// } /// ``` #[stable(feature = "io_error_inner", since = "1.3.0")] - pub fn get_mut(&mut self) -> Option<&mut (dyn error::Error + Send + Sync + 'static)> { + pub fn get_mut(&mut self) -> Option<&mut String> { match self.repr { Repr::Os(..) => None, Repr::Simple(..) => None, - Repr::Custom(ref mut c) => Some(&mut *c.error), + Repr::Custom(ref mut c) => Some(&mut c.error), } } @@ -479,7 +468,7 @@ impl Error { /// } /// ``` #[stable(feature = "io_error_inner", since = "1.3.0")] - pub fn into_inner(self) -> Option> { + pub fn into_inner(self) -> Option { match self.repr { Repr::Os(..) => None, Repr::Simple(..) => None, @@ -508,7 +497,7 @@ impl Error { #[stable(feature = "rust1", since = "1.0.0")] pub fn kind(&self) -> ErrorKind { match self.repr { - Repr::Os(code) => sys::decode_error_kind(code), + Repr::Os(_code) => ErrorKind::Other, Repr::Custom(ref c) => c.kind, Repr::Simple(kind) => kind, } @@ -521,8 +510,6 @@ impl fmt::Debug for Repr { Repr::Os(code) => fmt .debug_struct("Os") .field("code", &code) - .field("kind", &sys::decode_error_kind(code)) - .field("message", &sys::os::error_string(code)) .finish(), Repr::Custom(ref c) => fmt::Debug::fmt(&c, fmt), Repr::Simple(kind) => fmt.debug_tuple("Kind").field(&kind).finish(), @@ -535,8 +522,7 @@ impl fmt::Display for Error { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { match self.repr { Repr::Os(code) => { - let detail = sys::os::error_string(code); - write!(fmt, "{} (os error {})", detail, code) + write!(fmt, "os error {}", code) } Repr::Custom(ref c) => c.error.fmt(fmt), Repr::Simple(kind) => write!(fmt, "{}", kind.as_str()), @@ -544,34 +530,6 @@ impl fmt::Display for Error { } } -#[stable(feature = "rust1", since = "1.0.0")] -impl error::Error for Error { - #[allow(deprecated, deprecated_in_future)] - fn description(&self) -> &str { - match self.repr { - Repr::Os(..) | Repr::Simple(..) => self.kind().as_str(), - Repr::Custom(ref c) => c.error.description(), - } - } - - #[allow(deprecated)] - fn cause(&self) -> Option<&dyn error::Error> { - match self.repr { - Repr::Os(..) => None, - Repr::Simple(..) => None, - Repr::Custom(ref c) => c.error.cause(), - } - } - - fn source(&self) -> Option<&(dyn error::Error + 'static)> { - match self.repr { - Repr::Os(..) => None, - Repr::Simple(..) => None, - Repr::Custom(ref c) => c.error.source(), - } - } -} - fn _assert_error_is_sync_send() { fn _is_sync_send() {} _is_sync_send::(); diff --git a/library/alloc/src/io/mod.rs b/library/alloc/src/io/mod.rs index e696de34d0173..454377b98da1b 100644 --- a/library/alloc/src/io/mod.rs +++ b/library/alloc/src/io/mod.rs @@ -293,7 +293,7 @@ pub mod prelude; mod util; use crate::{vec::Vec, string::String}; -const DEFAULT_BUF_SIZE: usize = crate::sys_common::io::DEFAULT_BUF_SIZE; +const DEFAULT_BUF_SIZE: usize = 8 * 1024; struct Guard<'a> { buf: &'a mut Vec, @@ -1003,7 +1003,10 @@ pub fn read_to_string(reader: &mut R) -> Result { /// Windows. #[stable(feature = "iovec", since = "1.36.0")] #[repr(transparent)] -pub struct IoSliceMut<'a>(sys::io::IoSliceMut<'a>); +pub struct IoSliceMut<'a> { + vec: iovec, + _p: PhantomData<&'a [u8]>, +} #[stable(feature = "iovec-send-sync", since = "1.44.0")] unsafe impl<'a> Send for IoSliceMut<'a> {} @@ -1014,7 +1017,7 @@ unsafe impl<'a> Sync for IoSliceMut<'a> {} #[stable(feature = "iovec", since = "1.36.0")] impl<'a> fmt::Debug for IoSliceMut<'a> { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt::Debug::fmt(self.0.as_slice(), fmt) + fmt::Debug::fmt(self.as_slice(), fmt) } } @@ -1027,7 +1030,13 @@ impl<'a> IoSliceMut<'a> { #[stable(feature = "iovec", since = "1.36.0")] #[inline] pub fn new(buf: &'a mut [u8]) -> IoSliceMut<'a> { - IoSliceMut(sys::io::IoSliceMut::new(buf)) + IoSliceMut { + vec: iovec { + iov_base: buf.as_mut_ptr() as *mut c_void, + iov_len: buf.len() as iov_len_t, + }, + _p: PhantomData, + } } /// Advance the internal cursor of the slice. @@ -1080,10 +1089,34 @@ impl<'a> IoSliceMut<'a> { let bufs = &mut bufs[remove..]; if !bufs.is_empty() { - bufs[0].0.advance(n - accumulated_len) + bufs[0].advance_self(n - accumulated_len) } bufs } + + #[inline] + fn advance_self(&mut self, n: usize) { + if (self.vec.iov_len as usize) < n { + panic!("advancing IoSliceMut beyond its length"); + } + + unsafe { + self.vec.iov_len -= n as iov_len_t; + self.vec.iov_base = self.vec.iov_base.add(n); + } + } + + #[inline] + fn as_slice(&self) -> &[u8] { + unsafe { slice::from_raw_parts(self.vec.iov_base as *mut u8, self.vec.iov_len as usize) } + } + + #[inline] + fn as_mut_slice(&mut self) -> &mut [u8] { + unsafe { + slice::from_raw_parts_mut(self.vec.iov_base as *mut u8, self.vec.iov_len as usize) + } + } } #[stable(feature = "iovec", since = "1.36.0")] @@ -1092,7 +1125,7 @@ impl<'a> Deref for IoSliceMut<'a> { #[inline] fn deref(&self) -> &[u8] { - self.0.as_slice() + self.as_slice() } } @@ -1100,7 +1133,7 @@ impl<'a> Deref for IoSliceMut<'a> { impl<'a> DerefMut for IoSliceMut<'a> { #[inline] fn deref_mut(&mut self) -> &mut [u8] { - self.0.as_mut_slice() + self.as_mut_slice() } } @@ -1112,7 +1145,10 @@ impl<'a> DerefMut for IoSliceMut<'a> { #[stable(feature = "iovec", since = "1.36.0")] #[derive(Copy, Clone)] #[repr(transparent)] -pub struct IoSlice<'a>(sys::io::IoSlice<'a>); +pub struct IoSlice<'a> { + vec: iovec, + _p: PhantomData<&'a [u8]>, +} #[stable(feature = "iovec-send-sync", since = "1.44.0")] unsafe impl<'a> Send for IoSlice<'a> {} @@ -1123,7 +1159,7 @@ unsafe impl<'a> Sync for IoSlice<'a> {} #[stable(feature = "iovec", since = "1.36.0")] impl<'a> fmt::Debug for IoSlice<'a> { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt::Debug::fmt(self.0.as_slice(), fmt) + fmt::Debug::fmt(self.as_slice(), fmt) } } @@ -1136,7 +1172,13 @@ impl<'a> IoSlice<'a> { #[stable(feature = "iovec", since = "1.36.0")] #[inline] pub fn new(buf: &'a [u8]) -> IoSlice<'a> { - IoSlice(sys::io::IoSlice::new(buf)) + IoSlice { + vec: iovec { + iov_base: buf.as_ptr() as *mut u8 as *mut c_void, + iov_len: buf.len() as iov_len_t, + }, + _p: PhantomData, + } } /// Advance the internal cursor of the slice. @@ -1188,10 +1230,27 @@ impl<'a> IoSlice<'a> { let bufs = &mut bufs[remove..]; if !bufs.is_empty() { - bufs[0].0.advance(n - accumulated_len) + bufs[0].advance_self(n - accumulated_len) } bufs } + + #[inline] + fn advance_self(&mut self, n: usize) { + if (self.vec.iov_len as usize) < n { + panic!("advancing IoSlice beyond its length"); + } + + unsafe { + self.vec.iov_len -= n as iov_len_t; + self.vec.iov_base = self.vec.iov_base.add(n); + } + } + + #[inline] + fn as_slice(&self) -> &[u8] { + unsafe { slice::from_raw_parts(self.vec.iov_base as *mut u8, self.vec.iov_len as usize) } + } } #[stable(feature = "iovec", since = "1.36.0")] @@ -1200,7 +1259,7 @@ impl<'a> Deref for IoSlice<'a> { #[inline] fn deref(&self) -> &[u8] { - self.0.as_slice() + self.as_slice() } }