Skip to content

Commit 12c5065

Browse files
authored
Rollup merge of #81975 - Amanieu:seal2, r=m-ou-se
Seal the CommandExt, OsStrExt and OsStringExt traits A crater run (#81213 (comment)) has shown that this does not break any existing code. This also unblocks #77728. Based on #81213. r? ``@m-ou-se`` cc ``@lygstate``
2 parents 67cc5e4 + bfd1ccf commit 12c5065

File tree

7 files changed

+54
-30
lines changed

7 files changed

+54
-30
lines changed

library/std/src/ffi/os_str.rs

+8
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ pub struct OsString {
7676
inner: Buf,
7777
}
7878

79+
/// Allows extension traits within `std`.
80+
#[unstable(feature = "sealed", issue = "none")]
81+
impl crate::sealed::Sealed for OsString {}
82+
7983
/// Borrowed reference to an OS string (see [`OsString`]).
8084
///
8185
/// This type represents a borrowed reference to a string in the operating system's preferred
@@ -100,6 +104,10 @@ pub struct OsStr {
100104
inner: Slice,
101105
}
102106

107+
/// Allows extension traits within `std`.
108+
#[unstable(feature = "sealed", issue = "none")]
109+
impl crate::sealed::Sealed for OsStr {}
110+
103111
impl OsString {
104112
/// Constructs a new empty `OsString`.
105113
///

library/std/src/lib.rs

+8
Original file line numberDiff line numberDiff line change
@@ -582,3 +582,11 @@ include!("keyword_docs.rs");
582582
// is unconditional, so the unstable feature needs to be defined somewhere.
583583
#[unstable(feature = "restricted_std", issue = "none")]
584584
mod __restricted_std_workaround {}
585+
586+
mod sealed {
587+
/// This trait being unreachable from outside the crate
588+
/// prevents outside implementations of our extension traits.
589+
/// This allows adding more trait methods in the future.
590+
#[unstable(feature = "sealed", issue = "none")]
591+
pub trait Sealed {}
592+
}

library/std/src/process.rs

+8
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,10 @@ pub struct Command {
498498
inner: imp::Command,
499499
}
500500

501+
/// Allows extension traits within `std`.
502+
#[unstable(feature = "sealed", issue = "none")]
503+
impl crate::sealed::Sealed for Command {}
504+
501505
impl Command {
502506
/// Constructs a new `Command` for launching the program at
503507
/// path `program`, with the following default configuration:
@@ -1375,6 +1379,10 @@ impl From<fs::File> for Stdio {
13751379
#[stable(feature = "process", since = "1.0.0")]
13761380
pub struct ExitStatus(imp::ExitStatus);
13771381

1382+
/// Allows extension traits within `std`.
1383+
#[unstable(feature = "sealed", issue = "none")]
1384+
impl crate::sealed::Sealed for ExitStatus {}
1385+
13781386
impl ExitStatus {
13791387
/// Was termination successful? Signal termination is not considered a
13801388
/// success, and success is defined as a zero exit status.

library/std/src/sys/unix/ext/process.rs

+6-13
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,16 @@ use crate::ffi::OsStr;
66
use crate::io;
77
use crate::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
88
use crate::process;
9+
use crate::sealed::Sealed;
910
use crate::sys;
1011
use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
1112

12-
mod private {
13-
/// This trait being unreachable from outside the crate
14-
/// prevents other implementations of the `ExitStatusExt` trait,
15-
/// which allows potentially adding more trait methods in the future.
16-
#[stable(feature = "none", since = "1.51.0")]
17-
pub trait Sealed {}
18-
}
19-
2013
/// Unix-specific extensions to the [`process::Command`] builder.
14+
///
15+
/// This trait is sealed: it cannot be implemented outside the standard library.
16+
/// This is so that future additional methods are not breaking changes.
2117
#[stable(feature = "rust1", since = "1.0.0")]
22-
pub trait CommandExt {
18+
pub trait CommandExt: Sealed {
2319
/// Sets the child process's user ID. This translates to a
2420
/// `setuid` call in the child process. Failure in the `setuid`
2521
/// call will cause the spawn to fail.
@@ -193,7 +189,7 @@ impl CommandExt for process::Command {
193189
/// This trait is sealed: it cannot be implemented outside the standard library.
194190
/// This is so that future additional methods are not breaking changes.
195191
#[stable(feature = "rust1", since = "1.0.0")]
196-
pub trait ExitStatusExt: private::Sealed {
192+
pub trait ExitStatusExt: Sealed {
197193
/// Creates a new `ExitStatus` from the raw underlying `i32` return value of
198194
/// a process.
199195
#[stable(feature = "exit_status_from", since = "1.12.0")]
@@ -228,9 +224,6 @@ pub trait ExitStatusExt: private::Sealed {
228224
fn into_raw(self) -> i32;
229225
}
230226

231-
#[stable(feature = "none", since = "1.51.0")]
232-
impl private::Sealed for process::ExitStatus {}
233-
234227
#[stable(feature = "rust1", since = "1.0.0")]
235228
impl ExitStatusExt for process::ExitStatus {
236229
fn from_raw(raw: i32) -> Self {

library/std/src/sys/windows/ext/ffi.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#![stable(feature = "rust1", since = "1.0.0")]
5454

5555
use crate::ffi::{OsStr, OsString};
56+
use crate::sealed::Sealed;
5657
use crate::sys::os_str::Buf;
5758
use crate::sys_common::wtf8::Wtf8Buf;
5859
use crate::sys_common::{AsInner, FromInner};
@@ -61,8 +62,11 @@ use crate::sys_common::{AsInner, FromInner};
6162
pub use crate::sys_common::wtf8::EncodeWide;
6263

6364
/// Windows-specific extensions to [`OsString`].
65+
///
66+
/// This trait is sealed: it cannot be implemented outside the standard library.
67+
/// This is so that future additional methods are not breaking changes.
6468
#[stable(feature = "rust1", since = "1.0.0")]
65-
pub trait OsStringExt {
69+
pub trait OsStringExt: Sealed {
6670
/// Creates an `OsString` from a potentially ill-formed UTF-16 slice of
6771
/// 16-bit code units.
6872
///
@@ -92,8 +96,11 @@ impl OsStringExt for OsString {
9296
}
9397

9498
/// Windows-specific extensions to [`OsStr`].
99+
///
100+
/// This trait is sealed: it cannot be implemented outside the standard library.
101+
/// This is so that future additional methods are not breaking changes.
95102
#[stable(feature = "rust1", since = "1.0.0")]
96-
pub trait OsStrExt {
103+
pub trait OsStrExt: Sealed {
97104
/// Re-encodes an `OsStr` as a wide character sequence, i.e., potentially
98105
/// ill-formed UTF-16.
99106
///

library/std/src/sys/windows/ext/process.rs

+6-13
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,10 @@
44

55
use crate::os::windows::io::{AsRawHandle, FromRawHandle, IntoRawHandle, RawHandle};
66
use crate::process;
7+
use crate::sealed::Sealed;
78
use crate::sys;
89
use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
910

10-
mod private {
11-
/// This trait being unreachable from outside the crate
12-
/// prevents other implementations of the `ExitStatusExt` trait,
13-
/// which allows potentially adding more trait methods in the future.
14-
#[stable(feature = "none", since = "1.51.0")]
15-
pub trait Sealed {}
16-
}
17-
1811
#[stable(feature = "process_extensions", since = "1.2.0")]
1912
impl FromRawHandle for process::Stdio {
2013
unsafe fn from_raw_handle(handle: RawHandle) -> process::Stdio {
@@ -85,7 +78,7 @@ impl IntoRawHandle for process::ChildStderr {
8578
/// This trait is sealed: it cannot be implemented outside the standard library.
8679
/// This is so that future additional methods are not breaking changes.
8780
#[stable(feature = "exit_status_from", since = "1.12.0")]
88-
pub trait ExitStatusExt: private::Sealed {
81+
pub trait ExitStatusExt: Sealed {
8982
/// Creates a new `ExitStatus` from the raw underlying `u32` return value of
9083
/// a process.
9184
#[stable(feature = "exit_status_from", since = "1.12.0")]
@@ -99,12 +92,12 @@ impl ExitStatusExt for process::ExitStatus {
9992
}
10093
}
10194

102-
#[stable(feature = "none", since = "1.51.0")]
103-
impl private::Sealed for process::ExitStatus {}
104-
10595
/// Windows-specific extensions to the [`process::Command`] builder.
96+
///
97+
/// This trait is sealed: it cannot be implemented outside the standard library.
98+
/// This is so that future additional methods are not breaking changes.
10699
#[stable(feature = "windows_process_extensions", since = "1.16.0")]
107-
pub trait CommandExt {
100+
pub trait CommandExt: Sealed {
108101
/// Sets the [process creation flags][1] to be passed to `CreateProcess`.
109102
///
110103
/// These will always be ORed with `CREATE_UNICODE_ENVIRONMENT`.

library/std/src/sys_common/os_str_bytes.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::ffi::{OsStr, OsString};
66
use crate::fmt;
77
use crate::mem;
88
use crate::rc::Rc;
9+
use crate::sealed::Sealed;
910
use crate::str;
1011
use crate::sync::Arc;
1112
use crate::sys_common::bytestring::debug_fmt_bytestring;
@@ -232,8 +233,11 @@ impl Slice {
232233
}
233234

234235
/// Platform-specific extensions to [`OsString`].
236+
///
237+
/// This trait is sealed: it cannot be implemented outside the standard library.
238+
/// This is so that future additional methods are not breaking changes.
235239
#[stable(feature = "rust1", since = "1.0.0")]
236-
pub trait OsStringExt {
240+
pub trait OsStringExt: Sealed {
237241
/// Creates an [`OsString`] from a byte vector.
238242
///
239243
/// See the module documentation for an example.
@@ -258,8 +262,11 @@ impl OsStringExt for OsString {
258262
}
259263

260264
/// Platform-specific extensions to [`OsStr`].
265+
///
266+
/// This trait is sealed: it cannot be implemented outside the standard library.
267+
/// This is so that future additional methods are not breaking changes.
261268
#[stable(feature = "rust1", since = "1.0.0")]
262-
pub trait OsStrExt {
269+
pub trait OsStrExt: Sealed {
263270
#[stable(feature = "rust1", since = "1.0.0")]
264271
/// Creates an [`OsStr`] from a byte slice.
265272
///

0 commit comments

Comments
 (0)