about summary refs log tree commit diff
path: root/library/std/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-08-29 18:56:59 +0000
committerbors <bors@rust-lang.org>2022-08-29 18:56:59 +0000
commitbc4b39c271bbd36736cbf1c0a1ac23d5df38d365 (patch)
tree8f778be3486f10079fcf7448bc7601752152a5e6 /library/std/src
parentfcc2bddd262b26c3a4aeff2328e39b8e2b6d3254 (diff)
parent0b6faca67057ceb99ee3af342d4ba6f1ac407fde (diff)
downloadrust-bc4b39c271bbd36736cbf1c0a1ac23d5df38d365.tar.gz
rust-bc4b39c271bbd36736cbf1c0a1ac23d5df38d365.zip
Auto merge of #101152 - Dylan-DPC:rollup-v4iw8ux, r=Dylan-DPC
Rollup of 8 pull requests

Successful merges:

 - #98304 (Add MaybeUninit memset test)
 - #98801 (Add a `File::create_new` constructor)
 - #99821 (Remove separate indexing of early-bound regions)
 - #100239 (remove an ineffective check in const_prop)
 - #100337 (Stabilize `std::io::read_to_string`)
 - #100819 (Make use of `[wrapping_]byte_{add,sub}`)
 - #100934 (Remove a panicking branch from `fmt::builders::PadAdapter`)
 - #101000 (Separate CountIsStar from CountIsParam in rustc_parse_format.)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
Diffstat (limited to 'library/std/src')
-rw-r--r--library/std/src/fs.rs29
-rw-r--r--library/std/src/io/error/repr_bitpacked.rs4
-rw-r--r--library/std/src/io/mod.rs4
-rw-r--r--library/std/src/lib.rs1
4 files changed, 33 insertions, 5 deletions
diff --git a/library/std/src/fs.rs b/library/std/src/fs.rs
index e1ab06b0d0f..28a2c99f7e5 100644
--- a/library/std/src/fs.rs
+++ b/library/std/src/fs.rs
@@ -377,6 +377,35 @@ impl File {
         OpenOptions::new().write(true).create(true).truncate(true).open(path.as_ref())
     }
 
+    /// Creates a new file in read-write mode; error if the file exists.
+    ///
+    /// This function will create a file if it does not exist, or return an error if it does. This
+    /// way, if the call succeeds, the file returned is guaranteed to be new.
+    ///
+    /// This option is useful because it is atomic. Otherwise between checking whether a file
+    /// exists and creating a new one, the file may have been created by another process (a TOCTOU
+    /// race condition / attack).
+    ///
+    /// This can also be written using
+    /// `File::options().read(true).write(true).create_new(true).open(...)`.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// #![feature(file_create_new)]
+    ///
+    /// use std::fs::File;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let mut f = File::create_new("foo.txt")?;
+    ///     Ok(())
+    /// }
+    /// ```
+    #[unstable(feature = "file_create_new", issue = "none")]
+    pub fn create_new<P: AsRef<Path>>(path: P) -> io::Result<File> {
+        OpenOptions::new().read(true).write(true).create_new(true).open(path.as_ref())
+    }
+
     /// Returns a new OpenOptions object.
     ///
     /// This function returns a new OpenOptions object that you can use to
diff --git a/library/std/src/io/error/repr_bitpacked.rs b/library/std/src/io/error/repr_bitpacked.rs
index 292bf4826fd..781ae03ad45 100644
--- a/library/std/src/io/error/repr_bitpacked.rs
+++ b/library/std/src/io/error/repr_bitpacked.rs
@@ -269,10 +269,10 @@ where
         }
         TAG_SIMPLE_MESSAGE => ErrorData::SimpleMessage(&*ptr.cast::<SimpleMessage>().as_ptr()),
         TAG_CUSTOM => {
-            // It would be correct for us to use `ptr::sub` here (see the
+            // It would be correct for us to use `ptr::byte_sub` here (see the
             // comment above the `wrapping_add` call in `new_custom` for why),
             // but it isn't clear that it makes a difference, so we don't.
-            let custom = ptr.as_ptr().cast::<u8>().wrapping_sub(TAG_CUSTOM).cast::<Custom>();
+            let custom = ptr.as_ptr().wrapping_byte_sub(TAG_CUSTOM).cast::<Custom>();
             ErrorData::Custom(make_custom(custom))
         }
         _ => {
diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs
index a9fa7b0a6ac..01a3873c75c 100644
--- a/library/std/src/io/mod.rs
+++ b/library/std/src/io/mod.rs
@@ -1037,8 +1037,6 @@ pub trait Read {
 /// # Examples
 ///
 /// ```no_run
-/// #![feature(io_read_to_string)]
-///
 /// # use std::io;
 /// fn main() -> io::Result<()> {
 ///     let stdin = io::read_to_string(io::stdin())?;
@@ -1047,7 +1045,7 @@ pub trait Read {
 ///     Ok(())
 /// }
 /// ```
-#[unstable(feature = "io_read_to_string", issue = "80218")]
+#[stable(feature = "io_read_to_string", since = "CURRENT_RUSTC_VERSION")]
 pub fn read_to_string<R: Read>(mut reader: R) -> Result<String> {
     let mut buf = String::new();
     reader.read_to_string(&mut buf)?;
diff --git a/library/std/src/lib.rs b/library/std/src/lib.rs
index f5dcdab4cd5..8936e11b8e0 100644
--- a/library/std/src/lib.rs
+++ b/library/std/src/lib.rs
@@ -300,6 +300,7 @@
 #![feature(panic_can_unwind)]
 #![feature(panic_info_message)]
 #![feature(panic_internals)]
+#![feature(pointer_byte_offsets)]
 #![feature(pointer_is_aligned)]
 #![feature(portable_simd)]
 #![feature(prelude_2024)]