Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Seal all extension traits #81213

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions library/std/src/ffi/os_str.rs
Expand Up @@ -76,6 +76,10 @@ pub struct OsString {
inner: Buf,
}

/// Allows extension traits within `std`.
#[unstable(feature = "sealed", issue = "none")]
impl crate::sealed::Sealed for OsString {}

/// Borrowed reference to an OS string (see [`OsString`]).
///
/// This type represents a borrowed reference to a string in the operating system's preferred
Expand All @@ -100,6 +104,10 @@ pub struct OsStr {
inner: Slice,
}

/// Allows extension traits within `std`.
#[unstable(feature = "sealed", issue = "none")]
impl crate::sealed::Sealed for OsStr {}

impl OsString {
/// Constructs a new empty `OsString`.
///
Expand Down
28 changes: 28 additions & 0 deletions library/std/src/fs.rs
Expand Up @@ -92,6 +92,10 @@ pub struct File {
inner: fs_imp::File,
}

/// Allows extension traits within `std`.
#[unstable(feature = "sealed", issue = "none")]
impl crate::sealed::Sealed for File {}

/// Metadata information about a file.
///
/// This structure is returned from the [`metadata`] or
Expand All @@ -102,6 +106,10 @@ pub struct File {
#[derive(Clone)]
pub struct Metadata(fs_imp::FileAttr);

/// Allows extension traits within `std`.
#[unstable(feature = "sealed", issue = "none")]
impl crate::sealed::Sealed for Metadata {}

/// Iterator over the entries in a directory.
///
/// This iterator is returned from the [`read_dir`] function of this module and
Expand All @@ -128,6 +136,10 @@ pub struct ReadDir(fs_imp::ReadDir);
#[stable(feature = "rust1", since = "1.0.0")]
pub struct DirEntry(fs_imp::DirEntry);

/// Allows extension traits within `std`.
#[unstable(feature = "sealed", issue = "none")]
impl crate::sealed::Sealed for DirEntry {}

/// Options and flags which can be used to configure how a file is opened.
///
/// This builder exposes the ability to configure how a [`File`] is opened and
Expand Down Expand Up @@ -167,6 +179,10 @@ pub struct DirEntry(fs_imp::DirEntry);
#[stable(feature = "rust1", since = "1.0.0")]
pub struct OpenOptions(fs_imp::OpenOptions);

/// Allows extension traits within `std`.
#[unstable(feature = "sealed", issue = "none")]
impl crate::sealed::Sealed for OpenOptions {}

/// Representation of the various permissions on a file.
///
/// This module only currently provides one bit of information,
Expand All @@ -179,12 +195,20 @@ pub struct OpenOptions(fs_imp::OpenOptions);
#[stable(feature = "rust1", since = "1.0.0")]
pub struct Permissions(fs_imp::FilePermissions);

/// Allows extension traits within `std`.
#[unstable(feature = "sealed", issue = "none")]
impl crate::sealed::Sealed for Permissions {}

/// A structure representing a type of file with accessors for each file type.
/// It is returned by [`Metadata::file_type`] method.
#[stable(feature = "file_type", since = "1.1.0")]
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
pub struct FileType(fs_imp::FileType);

/// Allows extension traits within `std`.
#[unstable(feature = "sealed", issue = "none")]
impl crate::sealed::Sealed for FileType {}

/// A builder used to create directories in various manners.
///
/// This builder also supports platform-specific options.
Expand All @@ -195,6 +219,10 @@ pub struct DirBuilder {
recursive: bool,
}

/// Allows extension traits within `std`.
#[unstable(feature = "sealed", issue = "none")]
impl crate::sealed::Sealed for DirBuilder {}

/// Indicates how large a buffer to pre-allocate before reading the entire file.
fn initial_buffer_size(file: &File) -> usize {
// Allocate one extra byte so the buffer doesn't need to grow before the
Expand Down
8 changes: 8 additions & 0 deletions library/std/src/lib.rs
Expand Up @@ -579,3 +579,11 @@ include!("keyword_docs.rs");
// is unconditional, so the unstable feature needs to be defined somewhere.
#[unstable(feature = "restricted_std", issue = "none")]
mod __restricted_std_workaround {}

mod sealed {
/// This trait being unreachable from outside the crate
/// prevents outside implementations of our extension traits.
/// This allows adding more trait methods in the future.
#[unstable(feature = "sealed", issue = "none")]
pub trait Sealed {}
}
3 changes: 2 additions & 1 deletion library/std/src/os/android/fs.rs
@@ -1,6 +1,7 @@
#![stable(feature = "metadata_ext", since = "1.1.0")]

use crate::fs::Metadata;
use crate::sealed::Sealed;
use crate::sys_common::AsInner;

#[allow(deprecated)]
Expand All @@ -10,7 +11,7 @@ use crate::os::android::raw;
///
/// [`fs::Metadata`]: crate::fs::Metadata
#[stable(feature = "metadata_ext", since = "1.1.0")]
pub trait MetadataExt {
pub trait MetadataExt: Sealed {
/// Gain a reference to the underlying `stat` structure which contains
/// the raw information returned by the OS.
///
Expand Down
3 changes: 2 additions & 1 deletion library/std/src/os/dragonfly/fs.rs
@@ -1,6 +1,7 @@
#![stable(feature = "metadata_ext", since = "1.1.0")]

use crate::fs::Metadata;
use crate::sealed::Sealed;
use crate::sys_common::AsInner;

#[allow(deprecated)]
Expand All @@ -10,7 +11,7 @@ use crate::os::dragonfly::raw;
///
/// [`fs::Metadata`]: crate::fs::Metadata
#[stable(feature = "metadata_ext", since = "1.1.0")]
pub trait MetadataExt {
pub trait MetadataExt: Sealed {
/// Gain a reference to the underlying `stat` structure which contains
/// the raw information returned by the OS.
///
Expand Down
3 changes: 2 additions & 1 deletion library/std/src/os/emscripten/fs.rs
@@ -1,6 +1,7 @@
#![stable(feature = "metadata_ext", since = "1.1.0")]

use crate::fs::Metadata;
use crate::sealed::Sealed;
use crate::sys_common::AsInner;

#[allow(deprecated)]
Expand All @@ -10,7 +11,7 @@ use crate::os::emscripten::raw;
///
/// [`fs::Metadata`]: crate::fs::Metadata
#[stable(feature = "metadata_ext", since = "1.1.0")]
pub trait MetadataExt {
pub trait MetadataExt: Sealed {
/// Gain a reference to the underlying `stat` structure which contains
/// the raw information returned by the OS.
///
Expand Down
3 changes: 2 additions & 1 deletion library/std/src/os/freebsd/fs.rs
@@ -1,6 +1,7 @@
#![stable(feature = "metadata_ext", since = "1.1.0")]

use crate::fs::Metadata;
use crate::sealed::Sealed;
use crate::sys_common::AsInner;

#[allow(deprecated)]
Expand All @@ -10,7 +11,7 @@ use crate::os::freebsd::raw;
///
/// [`fs::Metadata`]: crate::fs::Metadata
#[stable(feature = "metadata_ext", since = "1.1.0")]
pub trait MetadataExt {
pub trait MetadataExt: Sealed {
/// Gain a reference to the underlying `stat` structure which contains
/// the raw information returned by the OS.
///
Expand Down
3 changes: 2 additions & 1 deletion library/std/src/os/fuchsia/fs.rs
@@ -1,13 +1,14 @@
#![stable(feature = "metadata_ext", since = "1.1.0")]

use crate::fs::Metadata;
use crate::sealed::Sealed;
use crate::sys_common::AsInner;

/// OS-specific extensions to [`fs::Metadata`].
///
/// [`fs::Metadata`]: crate::fs::Metadata
#[stable(feature = "metadata_ext", since = "1.1.0")]
pub trait MetadataExt {
pub trait MetadataExt: Sealed {
#[stable(feature = "metadata_ext2", since = "1.8.0")]
fn st_dev(&self) -> u64;
#[stable(feature = "metadata_ext2", since = "1.8.0")]
Expand Down
3 changes: 2 additions & 1 deletion library/std/src/os/haiku/fs.rs
@@ -1,6 +1,7 @@
#![stable(feature = "metadata_ext", since = "1.1.0")]

use crate::fs::Metadata;
use crate::sealed::Sealed;
use crate::sys_common::AsInner;

#[allow(deprecated)]
Expand All @@ -10,7 +11,7 @@ use crate::os::haiku::raw;
///
/// [`fs::Metadata`]: crate::fs::Metadata
#[stable(feature = "metadata_ext", since = "1.1.0")]
pub trait MetadataExt {
pub trait MetadataExt: Sealed {
/// Gain a reference to the underlying `stat` structure which contains
/// the raw information returned by the OS.
///
Expand Down
3 changes: 2 additions & 1 deletion library/std/src/os/illumos/fs.rs
@@ -1,6 +1,7 @@
#![stable(feature = "metadata_ext", since = "1.1.0")]

use crate::fs::Metadata;
use crate::sealed::Sealed;
use crate::sys_common::AsInner;

#[allow(deprecated)]
Expand All @@ -10,7 +11,7 @@ use crate::os::illumos::raw;
///
/// [`fs::Metadata`]: crate::fs::Metadata
#[stable(feature = "metadata_ext", since = "1.1.0")]
pub trait MetadataExt {
pub trait MetadataExt: Sealed {
/// Gain a reference to the underlying `stat` structure which contains
/// the raw information returned by the OS.
///
Expand Down
3 changes: 2 additions & 1 deletion library/std/src/os/ios/fs.rs
@@ -1,6 +1,7 @@
#![stable(feature = "metadata_ext", since = "1.1.0")]

use crate::fs::Metadata;
use crate::sealed::Sealed;
use crate::sys_common::AsInner;

#[allow(deprecated)]
Expand All @@ -10,7 +11,7 @@ use crate::os::ios::raw;
///
/// [`fs::Metadata`]: crate::fs::Metadata
#[stable(feature = "metadata_ext", since = "1.1.0")]
pub trait MetadataExt {
pub trait MetadataExt: Sealed {
/// Gain a reference to the underlying `stat` structure which contains
/// the raw information returned by the OS.
///
Expand Down
3 changes: 2 additions & 1 deletion library/std/src/os/linux/fs.rs
Expand Up @@ -3,6 +3,7 @@
#![stable(feature = "metadata_ext", since = "1.1.0")]

use crate::fs::Metadata;
use crate::sealed::Sealed;
use crate::sys_common::AsInner;

#[allow(deprecated)]
Expand All @@ -12,7 +13,7 @@ use crate::os::linux::raw;
///
/// [`fs::Metadata`]: crate::fs::Metadata
#[stable(feature = "metadata_ext", since = "1.1.0")]
pub trait MetadataExt {
pub trait MetadataExt: Sealed {
/// Gain a reference to the underlying `stat` structure which contains
/// the raw information returned by the OS.
///
Expand Down
3 changes: 2 additions & 1 deletion library/std/src/os/macos/fs.rs
@@ -1,6 +1,7 @@
#![stable(feature = "metadata_ext", since = "1.1.0")]

use crate::fs::Metadata;
use crate::sealed::Sealed;
use crate::sys_common::AsInner;

#[allow(deprecated)]
Expand All @@ -10,7 +11,7 @@ use crate::os::macos::raw;
///
/// [`fs::Metadata`]: crate::fs::Metadata
#[stable(feature = "metadata_ext", since = "1.1.0")]
pub trait MetadataExt {
pub trait MetadataExt: Sealed {
/// Gain a reference to the underlying `stat` structure which contains
/// the raw information returned by the OS.
///
Expand Down
3 changes: 2 additions & 1 deletion library/std/src/os/netbsd/fs.rs
@@ -1,6 +1,7 @@
#![stable(feature = "metadata_ext", since = "1.1.0")]

use crate::fs::Metadata;
use crate::sealed::Sealed;
use crate::sys_common::AsInner;

#[allow(deprecated)]
Expand All @@ -10,7 +11,7 @@ use crate::os::netbsd::raw;
///
/// [`fs::Metadata`]: crate::fs::Metadata
#[stable(feature = "metadata_ext", since = "1.1.0")]
pub trait MetadataExt {
pub trait MetadataExt: Sealed {
/// Gain a reference to the underlying `stat` structure which contains
/// the raw information returned by the OS.
///
Expand Down
3 changes: 2 additions & 1 deletion library/std/src/os/openbsd/fs.rs
@@ -1,6 +1,7 @@
#![stable(feature = "metadata_ext", since = "1.1.0")]

use crate::fs::Metadata;
use crate::sealed::Sealed;
use crate::sys_common::AsInner;

#[allow(deprecated)]
Expand All @@ -10,7 +11,7 @@ use crate::os::openbsd::raw;
///
/// [`fs::Metadata`]: crate::fs::Metadata
#[stable(feature = "metadata_ext", since = "1.1.0")]
pub trait MetadataExt {
pub trait MetadataExt: Sealed {
/// Gain a reference to the underlying `stat` structure which contains
/// the raw information returned by the OS.
///
Expand Down
3 changes: 2 additions & 1 deletion library/std/src/os/redox/fs.rs
@@ -1,6 +1,7 @@
#![stable(feature = "metadata_ext", since = "1.1.0")]

use crate::fs::Metadata;
use crate::sealed::Sealed;
use crate::sys_common::AsInner;

#[allow(deprecated)]
Expand All @@ -10,7 +11,7 @@ use crate::os::redox::raw;
///
/// [`fs::Metadata`]: crate::fs::Metadata
#[stable(feature = "metadata_ext", since = "1.1.0")]
pub trait MetadataExt {
pub trait MetadataExt: Sealed {
/// Gain a reference to the underlying `stat` structure which contains
/// the raw information returned by the OS.
///
Expand Down
3 changes: 2 additions & 1 deletion library/std/src/os/solaris/fs.rs
@@ -1,6 +1,7 @@
#![stable(feature = "metadata_ext", since = "1.1.0")]

use crate::fs::Metadata;
use crate::sealed::Sealed;
use crate::sys_common::AsInner;

#[allow(deprecated)]
Expand All @@ -10,7 +11,7 @@ use crate::os::solaris::raw;
///
/// [`fs::Metadata`]: crate::fs::Metadata
#[stable(feature = "metadata_ext", since = "1.1.0")]
pub trait MetadataExt {
pub trait MetadataExt: Sealed {
/// Gain a reference to the underlying `stat` structure which contains
/// the raw information returned by the OS.
///
Expand Down
3 changes: 2 additions & 1 deletion library/std/src/os/vxworks/fs.rs
@@ -1,12 +1,13 @@
#![stable(feature = "metadata_ext", since = "1.1.0")]

use crate::fs::Metadata;
use crate::sealed::Sealed;
use crate::sys_common::AsInner;

///
/// [`fs::Metadata`]: crate::fs::Metadata
#[stable(feature = "metadata_ext", since = "1.1.0")]
pub trait MetadataExt {
pub trait MetadataExt: Sealed {
#[stable(feature = "metadata_ext2", since = "1.8.0")]
fn st_dev(&self) -> u64;
#[stable(feature = "metadata_ext2", since = "1.8.0")]
Expand Down
8 changes: 8 additions & 0 deletions library/std/src/process.rs
Expand Up @@ -498,6 +498,10 @@ pub struct Command {
inner: imp::Command,
}

/// Allows extension traits within `std`.
#[unstable(feature = "sealed", issue = "none")]
impl crate::sealed::Sealed for Command {}

impl Command {
/// Constructs a new `Command` for launching the program at
/// path `program`, with the following default configuration:
Expand Down Expand Up @@ -1375,6 +1379,10 @@ impl From<fs::File> for Stdio {
#[stable(feature = "process", since = "1.0.0")]
pub struct ExitStatus(imp::ExitStatus);

/// Allows extension traits within `std`.
#[unstable(feature = "sealed", issue = "none")]
impl crate::sealed::Sealed for ExitStatus {}

impl ExitStatus {
/// Was termination successful? Signal termination is not considered a
/// success, and success is defined as a zero exit status.
Expand Down