about summary refs log tree commit diff
path: root/library/std/src
AgeCommit message (Collapse)AuthorLines
2023-12-03Auto merge of #118487 - RalfJung:exposed-provenance, r=thomccbors-0/+1
move exposed-provenance APIs into separate feature gate We have already stated explicitly for all the 'exposed' functions that > Using this method means that code is *not* following strict provenance rules. However, they were part of the same feature gate and still described as part of the strict provenance experiment. Unfortunately, their semantics are much less clear and certainly nowhere near stabilization, so in preparation for an attempt to stabilize the strict provenance APIs, I suggest we split the things related to "exposed" into their own feature gate. I also used this opportunity to better explain how Exposed Provenance fits into the larger plan here: this is *one possible candidate* for `as` semantics, but we don't know if it is actually viable, so we can't really promise that it is equivalent to `as`. If it works out we probably want to make `as` equivalent to the 'exposed' APIs; if it doesn't, we will remove them again and try to find some other semantics for `as`.
2023-12-02Auto merge of #118484 - blyxxyz:os-str-slice, r=BurntSushibors-2/+131
Add substring API for `OsStr` This adds a method for taking a substring of an `OsStr`, which in combination with [`OsStr::as_encoded_bytes()`](https://doc.rust-lang.org/std/ffi/struct.OsStr.html#method.as_encoded_bytes) makes it possible to implement most string operations in safe code. API: ```rust impl OsStr { pub fn slice_encoded_bytes<R: ops::RangeBounds<usize>>(&self, range: R) -> &Self; } ``` Motivation, examples and research at https://github.com/rust-lang/libs-team/issues/306. Tracking issue: #118485 cc `@epage` r? libs-api
2023-12-02std: Invert logic for inclusion of `sys_common::net`Alex Crichton-8/+8
The `library/std/src/sys_common/net.rs` module is intended to define common implementations of networking-related APIs across a variety of platforms that share similar APIs (e.g. Berkeley-style sockets and all). This module is not included for more fringe targets however such as UEFI or "unknown" targets to libstd (those classified as `restricted-std`). Previously the `sys_common/net.rs` file was set up such that an allow-list indicated it shouldn't be used. This commit inverts the logic to have an allow-list of when it should be used instead. The goal of this commit is to make it a bit easier to experiment with a new Rust target. Currently more esoteric targets are required to get an exception in this `cfg_if` block to use `crate::sys::net` such as for unsupported targets. With this inversion of logic only targets which actually support networking will be listed, where most of those are lumped under `cfg(unix)`. Given that this change is likely to cause some breakage for some target by accident I've attempted to be somewhat robust with this by following these steps to defining the new predicate for inverted logic. 1. Take all supported targets and filter out all `cfg(unix)` ones as these should all support `sys_common/net.rs`. 2. Take remaining targets and filter out `cfg(windows)` ones. 3. The remaining dozen-or-so targets were all audited by hand. Mostly this included `target_os = "hermit"` and `target_os = "solid_asp3"` which required an allow-list entry, but remaining targets were all already excluded (didn't use `sys_common/net.rs` so they were left out. If this causes breakage it should be relatively easy to fix and I'd be happy to follow-up with any PRs necessary.
2023-12-02Auto merge of #118077 - calebzulawski:sync-portable-simd-2023-11-19, ↵bors-1/+2
r=workingjubilee Portable SIMD subtree update Syncs nightly to the latest changes from rust-lang/portable-simd r? `@rust-lang/libs`
2023-12-02Add substring API for `OsStr`Jan Verbeek-2/+131
2023-12-01Auto merge of #117248 - ChrisDenton:ci-symlink, r=m-ou-sebors-1/+1
Error if symlinks are not supported in CI In CI we want to run as many tests as possible and be alerted if a test isn't run for any reason.
2023-11-30move exposed-provenance APIs into separate feature gate and explain the ↵Ralf Jung-0/+1
relationship of Exposed Provenance and Strict Provenance
2023-11-29Rollup merge of #116839 - joboet:xous_thread_parking, r=m-ou-seMatthias Krüger-1/+95
Implement thread parking for xous This follows the pattern set by [the Windows parker](https://github.com/rust-lang/rust/blob/ddef56d5dfa18f169af9db912dc8e8343797eebb/library/std/src/sys/windows/thread_parking.rs) when it uses keyed events. An atomic variable is used to track the state and optimize the fast path, while notifications are send via the ticktime server to block and unblock the thread. ping `@xobs` `@rustbot` label +T-libs +A-atomic r? libs
2023-11-28Rollup merge of #118222 - the8472:copy-use-vec-write, r=m-ou-seMatthias Krüger-107/+91
unify read_to_end and io::copy impls for reading into a Vec This ports over the initial probe (to avoid allocation) and the dynamic read sizing from the io::copy specialization to the `default_read_to_end` implementation which already had its own optimizations for different cases. I think it should be a best-of-both now. suggested by `@a1phyr` in https://github.com/rust-lang/rust/pull/117576#issuecomment-1803408492
2023-11-28Rollup merge of #118193 - max-heller:command-typo, r=m-ou-seMatthias Krüger-1/+1
Add missing period in `std::process::Command` docs
2023-11-28Rollup merge of #118299 - frewsxcv:patch-2, r=cuviperMatthias Krüger-9/+41
Update `OnceLock` documentation to give a concrete 'lazy static' example, and expand on the existing example.
2023-11-26unify read_to_end and io::copy impls for reading into a VecThe 8472-107/+91
2023-11-26Use inner docs to fix linksCaleb Zulawski-1/+2
2023-11-26Making `User<T>` and `User<[T]>` `Send`Raoul Strackx-0/+6
2023-11-26Clean dead codesr0cky-8/+0
2023-11-25Update `OnceLock` documentation to give a concrete 'lazy static' example, ↵Corey Farwell-9/+41
and expand on existing example.
2023-11-24Rollup merge of #118060 - ChrisDenton:abs-device-path, r=thomccMatthias Krüger-1/+1
Use an absolute path to the NUL device While a bare "NUL" *should* be redirected to the NUL device, especially in this simple case, let's be explicit that we aren't opening a file called "NUL" and instead open it directly. This will also set a good example for people copying std code. r? libs
2023-11-24Rollup merge of #117656 - ChrisDenton:invalid, r=thomccMatthias Krüger-7/+8
Update windows-bindgen and define `INVALID_HANDLE_VALUE` ourselves We generate bindings to the Windows API via the `windows-bindgen` crate, which is ultimately what's also used to generate the `windows-sys` and `windows` crates. However, there currently is some custom sauce just for std which makes it a bit different from the vanilla bindings. I would love for us to reduce and eventually remove the differences entirely so that std is using the exact same bindings as everyone else. Maybe in the future we can even just have a normal dependency on `windows-sys`. This PR removes one of those special things. Our definition of `INVALID_HANDLE_VALUE` relies on an experimental nightly feature for strict provenance, so lets bring that back in house. It also excludes it from the codegen step though that isn't strictly necessary as we override it in any case. This PR also updates windows-bingen to 0.52.0.
2023-11-24Rollup merge of #116807 - seanlinsley:patch-2, r=thomccMatthias Krüger-0/+4
Improve rewind documentation The persistent use of an internal cursor for readers is expected for buffer data types that aren't read all at once, but for files it leads to the confusing situation where calling `read_to_end` on the same file handle multiple times only returns the contents of the file for the first call. This PR adds a note to the documentation clarifying that in that case, `rewind()` must first be called. I'm unsure if this is the right location for the docs update. Maybe it should also be duplicated on `File`?
2023-11-23Auto merge of #98943 - WilliamVenner:feat/bufread_skip_until, r=dtolnaybors-0/+114
Add `BufRead::skip_until` Alternative version of `BufRead::read_until` that simply discards data, rather than copying it into a buffer. Useful for situations like skipping irrelevant data in a binary file format that is NUL-terminated. <details> <summary>Benchmark</summary> ``` running 2 tests test bench_read_until ... bench: 123 ns/iter (+/- 6) test bench_skip_until ... bench: 66 ns/iter (+/- 3) ``` ```rs #![feature(test)] extern crate test; use test::Bencher; use std::io::{ErrorKind, BufRead}; fn skip_until<R: BufRead + ?Sized>(r: &mut R, delim: u8) -> Result<usize, std::io::Error> { let mut read = 0; loop { let (done, used) = { let available = match r.fill_buf() { Ok(n) => n, Err(ref e) if e.kind() == ErrorKind::Interrupted => continue, Err(e) => return Err(e), }; match memchr::memchr(delim, available) { Some(i) => (true, i + 1), None => (false, available.len()), } }; r.consume(used); read += used; if done || used == 0 { return Ok(read); } } } const STR: &[u8] = b"Ferris\0Hello, world!\0"; #[bench] fn bench_skip_until(b: &mut Bencher) { b.iter(|| { let mut io = std::io::Cursor::new(test::black_box(STR)); skip_until(&mut io, b'\0').unwrap(); let mut hello = Vec::with_capacity(b"Hello, world!\0".len()); let num_bytes = io.read_until(b'\0', &mut hello).unwrap(); assert_eq!(num_bytes, b"Hello, world!\0".len()); assert_eq!(hello, b"Hello, world!\0"); }); } #[bench] fn bench_read_until(b: &mut Bencher) { b.iter(|| { let mut io = std::io::Cursor::new(test::black_box(STR)); io.read_until(b'\0', &mut Vec::new()).unwrap(); let mut hello = Vec::with_capacity(b"Hello, world!\0".len()); let num_bytes = io.read_until(b'\0', &mut hello).unwrap(); assert_eq!(num_bytes, b"Hello, world!\0".len()); assert_eq!(hello, b"Hello, world!\0"); }); } ``` </details>
2023-11-23Fix tracking issue of Windows ExitCodeExtDavid Tolnay-3/+3
2023-11-23Auto merge of #115159 - solid-rs:patch/kmc-solid/io-safety, r=workingjubileebors-132/+371
kmc-solid: I/O safety Adds the I/O safety API (#87329) for socket file descriptors in [`*-kmc-solid_*`](https://doc.rust-lang.org/nightly/rustc/platform-support/kmc-solid.html) Tier 3 targets. All new public items are gated by the `solid_ext` library feature. This PR adds the following public types and traits: std::os::solid::io::AsFd std::os::solid::io::BorrowedFd std::os::solid::io::OwnedFd std::os::solid::prelude::AsFd (re-export) std::os::solid::prelude::BorrowedFd (re-export) std::os::solid::prelude::OwnedFd (re-export) And trait implementations: From<std::net::TcpListener> for std::os::solid::io::OwnedFd From<std::net::TcpStream> for std::os::solid::io::OwnedFd From<std::net::UdpSocket> for std::os::solid::io::OwnedFd From<std::os::solid::io::OwnedFd> for std::net::TcpListener From<std::os::solid::io::OwnedFd> for std::net::TcpStream From<std::os::solid::io::OwnedFd> for std::net::UdpSocket std::fmt::Debug for std::os::solid::io::BorrowedFd<'_> std::fmt::Debug for std::os::solid::io::OwnedFd std::io::IsTerminal for std::os::solid::io::BorrowedFd<'_> std::io::IsTerminal for std::os::solid::io::OwnedFd std::os::fd::AsRawFd for std::os::solid::io::BorrowedFd<'_> std::os::fd::AsRawFd for std::os::solid::io::OwnedFd std::os::fd::FromRawFd for std::os::solid::io::OwnedFd std::os::fd::IntoRawFd for std::os::solid::io::OwnedFd std::os::solid::io::AsFd for &impl std::os::solid::io::AsFd std::os::solid::io::AsFd for &mut impl std::os::solid::io::AsFd std::os::solid::io::AsFd for Arc<impl std::os::solid::io::AsFd> std::os::solid::io::AsFd for Box<impl std::os::solid::io::AsFd> std::os::solid::io::AsFd for Rc<impl std::os::solid::io::AsFd> std::os::solid::io::AsFd for std::net::TcpListener std::os::solid::io::AsFd for std::net::TcpStream std::os::solid::io::AsFd for std::net::UdpSocket std::os::solid::io::AsFd for std::os::solid::io::BorrowedFd<'_> std::os::solid::io::AsFd for std::os::solid::io::OwnedFd Taking advantage of the above change, this PR also refactors the internal details of `std::sys::solid::net` to match the design of other targets, e.g., by redefining `Socket` as a newtype of `OwnedFd`.
2023-11-22add missing period in `std::process::Command` docsMax Heller-1/+1
2023-11-22x fmt library/stdChris Denton-3/+2
2023-11-22redundant_slicingChris Denton-1/+1
2023-11-22cmp_nullChris Denton-2/+2
comparing with null is better expressed by the `.is_null()` method
2023-11-22manual_range_containsChris Denton-2/+2
2023-11-22op_refChris Denton-1/+1
taken reference of right operand
2023-11-22manual_mapChris Denton-5/+3
manual implementation of `Option::map`
2023-11-22unnecessary_lazy_evaluationsChris Denton-1/+1
unnecessary closure used with `bool::then`
2023-11-22redundant_closureChris Denton-4/+4
2023-11-22duration_subsecChris Denton-1/+1
calling `subsec_micros()` is more concise than this calculation
2023-11-22unnecessary_castChris Denton-9/+9
casting to the same type is unnecessary
2023-11-22needless_borrowChris Denton-8/+8
this expression creates a reference which is immediately dereferenced by the compiler
2023-11-22needless_borrows_for_generic_argsChris Denton-2/+2
the borrowed expression implements the required traits
2023-11-22manual_slice_size_calculationChris Denton-1/+1
2023-11-22unnecessary_mut_passedChris Denton-4/+4
This is where our Windows API bindings previously (and incorrectly) used `*mut` instead of `*const` pointers. Now that the bindings have been corrected, the mutable references (which auto-convert to `*mut`) are unnecessary and we can use shared references.
2023-11-22useless_conversionChris Denton-1/+1
2023-11-22needless_returnChris Denton-1/+1
unneeded `return` statement
2023-11-22allow clippy style in windows/c.rsChris Denton-0/+1
We intentional use the Windows API style here.
2023-11-21Rollup merge of #117790 - rcvalle:rust-cfi-fix-000000, r=workingjubileeNilstrieb-0/+2
CFI: Add missing use core::ffi::c_int Adds missing use core::ffi::c_int for when sanitizer_cfi_normalize_integers is defined.
2023-11-19Rollup merge of #117957 - the8472:pidfd-wait, r=Mark-SimulacrumMichael Goulet-11/+90
if available use a Child's pidfd for kill/wait This should get us closer to stabilization of pidfds since they now do something useful. And they're `CLOEXEC` now. ``` $ strace -ffe clone,sendmsg,recvmsg,execve,kill,pidfd_open,pidfd_send_signal,waitpid,waitid ./x test std --no-doc -- pidfd [...] running 1 tests strace: Process 816007 attached [pid 816007] pidfd_open(816006, 0) = 3 [pid 816007] clone(child_stack=NULL, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f0c6b787990) = 816008 strace: Process 816008 attached [pid 816007] recvmsg(3, <unfinished ...> [pid 816008] pidfd_open(816008, 0) = 3 [pid 816008] sendmsg(4, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="", iov_len=0}], msg_iovlen=1, msg_control=[{cmsg_len=20, cmsg_level=SOL_SOCKET, cmsg_type=SCM_RIGHTS, cmsg_data=[3]}], msg_controllen=24, msg_flags=0}, 0) = 0 [pid 816007] <... recvmsg resumed>{msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="", iov_len=0}], msg_iovlen=1, msg_control=[{cmsg_len=20, cmsg_level=SOL_SOCKET, cmsg_type=SCM_RIGHTS, cmsg_data=[4]}], msg_controllen=24, msg_flags=MSG_CMSG_CLOEXEC}, MSG_CMSG_CLOEXEC) = 0 [pid 816008] execve("/usr/bin/false", ["false"], 0x7ffcf2100048 /* 105 vars */) = 0 [pid 816007] waitid(P_PIDFD, 4, <unfinished ...> [pid 816008] +++ exited with 1 +++ [pid 816007] <... waitid resumed>{si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=816008, si_uid=1001, si_status=1, si_utime=0, si_stime=0}, WEXITED, NULL) = 0 [pid 816007] --- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=816008, si_uid=1001, si_status=1, si_utime=0, si_stime=0} --- [pid 816007] clone(child_stack=NULL, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLDstrace: Process 816009 attached , child_tidptr=0x7f0c6b787990) = 816009 [pid 816007] recvmsg(3, <unfinished ...> [pid 816009] pidfd_open(816009, 0) = 3 [pid 816009] sendmsg(5, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="", iov_len=0}], msg_iovlen=1, msg_control=[{cmsg_len=20, cmsg_level=SOL_SOCKET, cmsg_type=SCM_RIGHTS, cmsg_data=[3]}], msg_controllen=24, msg_flags=0}, 0) = 0 [pid 816007] <... recvmsg resumed>{msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="", iov_len=0}], msg_iovlen=1, msg_control=[{cmsg_len=20, cmsg_level=SOL_SOCKET, cmsg_type=SCM_RIGHTS, cmsg_data=[5]}], msg_controllen=24, msg_flags=MSG_CMSG_CLOEXEC}, MSG_CMSG_CLOEXEC) = 0 [pid 816009] execve("/usr/bin/sleep", ["sleep", "1000"], 0x7ffcf2100048 /* 105 vars */) = 0 [pid 816007] waitid(P_PIDFD, 5, {}, WNOHANG|WEXITED, NULL) = 0 [pid 816007] pidfd_send_signal(5, SIGKILL, NULL, 0) = 0 [pid 816007] waitid(P_PIDFD, 5, <unfinished ...> [pid 816009] +++ killed by SIGKILL +++ [pid 816007] <... waitid resumed>{si_signo=SIGCHLD, si_code=CLD_KILLED, si_pid=816009, si_uid=1001, si_status=SIGKILL, si_utime=0, si_stime=0}, WEXITED, NULL) = 0 [pid 816007] --- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_KILLED, si_pid=816009, si_uid=1001, si_status=SIGKILL, si_utime=0, si_stime=0} --- [pid 816007] +++ exited with 0 +++ ```
2023-11-19Use an absolute path to the NUL deviceChris Denton-1/+1
While a bare "NUL" *should* be redirected to the NUL device, especially in this simple case, let's be explicit that we aren't opening a file called "NUL" and instead open it directly. This will also set a good example for people copying std code.
2023-11-19Don't set cmsg fields in msghdr if we have no cmsg to sendThe 8472-3/+4
2023-11-19Auto merge of #117895 - mzohreva:mz/fix-sgx-backtrace, r=Mark-Simulacrumbors-1/+14
Adjust frame IP in backtraces relative to image base for SGX target This is followup to https://github.com/rust-lang/backtrace-rs/pull/566. The backtraces printed by `panic!` or generated by `std::backtrace::Backtrace` in SGX target are not usable. The frame addresses need to be relative to image base address so they can be used for symbol resolution. Here's an example panic backtrace generated before this change: ``` $ cargo r --target x86_64-fortanix-unknown-sgx ... stack backtrace: 0: 0x7f8fe401d3a5 - <unknown> 1: 0x7f8fe4034780 - <unknown> 2: 0x7f8fe401c5a3 - <unknown> 3: 0x7f8fe401d1f5 - <unknown> 4: 0x7f8fe401e6f6 - <unknown> ``` Here's the same panic after this change: ``` $ cargo +stage1 r --target x86_64-fortanix-unknown-sgx stack backtrace: 0: 0x198bf - <unknown> 1: 0x3d181 - <unknown> 2: 0x26164 - <unknown> 3: 0x19705 - <unknown> 4: 0x1ef36 - <unknown> ``` cc `@jethrogb` and `@workingjubilee`
2023-11-19Rollup merge of #116750 - fintelia:seek_seek_relative, r=Mark-SimulacrumTakayuki Maeda-0/+40
Add Seek::seek_relative The `BufReader` struct has a `seek_relative` method because its `Seek::seek` implementation involved dumping the internal buffer (https://github.com/rust-lang/rust/issues/31100). Unfortunately, there isn't really a good way to take advantage of that method in generic code. This PR adds the same method to the main `Seek` trait with the straightforward default method, and an override for `BufReader` that calls its implementation. _Also discussed in [this](https://internals.rust-lang.org/t/add-seek-seek-relative/19546) internals.rust-lang.org thread._
2023-11-18Auto merge of #117138 - zachs18:rwlock_guard_debug_unsized, r=dtolnaybors-2/+2
Add T: ?Sized to `RwLockReadGuard` and `RwLockWriteGuard`'s Debug impls. For context, `MutexGuard` has `+ ?Sized` on its `Debug` impl, and all three have `+ ?Sized` on their `Display` impls. It looks like the `?Sized` was just missed when the impls were added (the impl for `MutexGuard` was added in the same PR (https://github.com/rust-lang/rust/pull/38006) with support for `T: Debug + ?Sized`, and `RwLock*Guard`s did allow `T: ?Sized` types already); the `Display` impls were added later (https://github.com/rust-lang/rust/pull/42822) with support for `T: Debug + ?Sized` types. I think this needs a T-libs-api FCP? I'm not sure if this also needs an ACP. If so I can make one. These are changes to (stable) trait impls on stable types so will be insta-stable. `@rustbot` label +T-libs-api
2023-11-17Rollup merge of #117338 - workingjubilee:asmjs-meets-thanatos, r=b-naberMatthias Krüger-14/+0
Remove asmjs Fulfills [MCP 668](https://github.com/rust-lang/compiler-team/issues/668). `asmjs-unknown-emscripten` does not work as-specified, and lacks essential upstream support for generating asm.js, so it should not exist at all.
2023-11-17Use ptr::invalid_mut for SGX image baseMohsen Zohrevandi-1/+1
2023-11-17Update windows-bindgenChris Denton-5/+5