From e447fd626d8c06e26513c15781e07bc33270e41e Mon Sep 17 00:00:00 2001 From: Raytwo Date: Sat, 28 Aug 2021 23:10:16 +0200 Subject: [PATCH] Add missing functions for the Nintendo Switch --- library/std/src/os/switch.rs | 7 --- library/std/src/os/switch/ffi/mod.rs | 45 ++++++++++++++++ library/std/src/os/switch/ffi/os_str.rs | 68 +++++++++++++++++++++++++ library/std/src/os/switch/mod.rs | 2 + library/std/src/sys/switch/rwlock.rs | 2 + library/std/src/sys/switch/thread.rs | 6 +++ 6 files changed, 123 insertions(+), 7 deletions(-) delete mode 100644 library/std/src/os/switch.rs create mode 100644 library/std/src/os/switch/ffi/mod.rs create mode 100644 library/std/src/os/switch/ffi/os_str.rs create mode 100644 library/std/src/os/switch/mod.rs diff --git a/library/std/src/os/switch.rs b/library/std/src/os/switch.rs deleted file mode 100644 index e68ae2e97a4..00000000000 --- a/library/std/src/os/switch.rs +++ /dev/null @@ -1,7 +0,0 @@ -#![stable(feature = "rust1", since = "1.0.0")] - -pub mod ffi { - #![stable(feature = "rust1", since = "1.0.0")] - #[stable(feature = "rust1", since = "1.0.0")] - pub use crate::sys_common::os_str_bytes::*; -} \ No newline at end of file diff --git a/library/std/src/os/switch/ffi/mod.rs b/library/std/src/os/switch/ffi/mod.rs new file mode 100644 index 00000000000..65e39ecfd00 --- /dev/null +++ b/library/std/src/os/switch/ffi/mod.rs @@ -0,0 +1,45 @@ +//! Unix-specific extension to the primitives in the `std::ffi` module. +//! +//! # Examples +//! +//! ``` +//! use std::ffi::OsString; +//! use std::os::unix::ffi::OsStringExt; +//! +//! let bytes = b"foo".to_vec(); +//! +//! // OsStringExt::from_vec +//! let os_string = OsString::from_vec(bytes); +//! assert_eq!(os_string.to_str(), Some("foo")); +//! +//! // OsStringExt::into_vec +//! let bytes = os_string.into_vec(); +//! assert_eq!(bytes, b"foo"); +//! ``` +//! +//! ``` +//! use std::ffi::OsStr; +//! use std::os::unix::ffi::OsStrExt; +//! +//! let bytes = b"foo"; +//! +//! // OsStrExt::from_bytes +//! let os_str = OsStr::from_bytes(bytes); +//! assert_eq!(os_str.to_str(), Some("foo")); +//! +//! // OsStrExt::as_bytes +//! let bytes = os_str.as_bytes(); +//! assert_eq!(bytes, b"foo"); +//! ``` + +#![stable(feature = "rust1", since = "1.0.0")] +mod os_str; + +#[stable(feature = "rust1", since = "1.0.0")] +pub use self::os_str::{OsStrExt, OsStringExt}; + +#[stable(feature = "rust1", since = "1.0.0")] +mod ffi { + #[stable(feature = "rust1", since = "1.0.0")] + pub use crate::sys_common::os_str_bytes::*; +} \ No newline at end of file diff --git a/library/std/src/os/switch/ffi/os_str.rs b/library/std/src/os/switch/ffi/os_str.rs new file mode 100644 index 00000000000..54c9a9382f2 --- /dev/null +++ b/library/std/src/os/switch/ffi/os_str.rs @@ -0,0 +1,68 @@ +use crate::ffi::{OsStr, OsString}; +use crate::mem; +use crate::sealed::Sealed; +use crate::sys::os_str::Buf; +use crate::sys_common::{AsInner, FromInner, IntoInner}; + +// Note: this file is currently reused in other `std::os::{platform}::ffi` modules to reduce duplication. +// Keep this in mind when applying changes to this file that only apply to `unix`. + +/// Platform-specific extensions to [`OsString`]. +/// +/// This trait is sealed: it cannot be implemented outside the standard library. +/// This is so that future additional methods are not breaking changes. +#[stable(feature = "rust1", since = "1.0.0")] +pub trait OsStringExt: Sealed { + /// Creates an [`OsString`] from a byte vector. + /// + /// See the module documentation for an example. + #[stable(feature = "rust1", since = "1.0.0")] + fn from_vec(vec: Vec) -> Self; + + /// Yields the underlying byte vector of this [`OsString`]. + /// + /// See the module documentation for an example. + #[stable(feature = "rust1", since = "1.0.0")] + fn into_vec(self) -> Vec; +} + +#[stable(feature = "rust1", since = "1.0.0")] +impl OsStringExt for OsString { + fn from_vec(vec: Vec) -> OsString { + FromInner::from_inner(Buf { inner: vec }) + } + fn into_vec(self) -> Vec { + self.into_inner().inner + } +} + +/// Platform-specific extensions to [`OsStr`]. +/// +/// This trait is sealed: it cannot be implemented outside the standard library. +/// This is so that future additional methods are not breaking changes. +#[stable(feature = "rust1", since = "1.0.0")] +pub trait OsStrExt: Sealed { + #[stable(feature = "rust1", since = "1.0.0")] + /// Creates an [`OsStr`] from a byte slice. + /// + /// See the module documentation for an example. + fn from_bytes(slice: &[u8]) -> &Self; + + /// Gets the underlying byte view of the [`OsStr`] slice. + /// + /// See the module documentation for an example. + #[stable(feature = "rust1", since = "1.0.0")] + fn as_bytes(&self) -> &[u8]; +} + +#[stable(feature = "rust1", since = "1.0.0")] +impl OsStrExt for OsStr { + #[inline] + fn from_bytes(slice: &[u8]) -> &OsStr { + unsafe { mem::transmute(slice) } + } + #[inline] + fn as_bytes(&self) -> &[u8] { + &self.as_inner().inner + } +} diff --git a/library/std/src/os/switch/mod.rs b/library/std/src/os/switch/mod.rs new file mode 100644 index 00000000000..c1b0597fc3b --- /dev/null +++ b/library/std/src/os/switch/mod.rs @@ -0,0 +1,2 @@ +#![stable(feature = "rust1", since = "1.0.0")] +pub mod ffi; \ No newline at end of file diff --git a/library/std/src/sys/switch/rwlock.rs b/library/std/src/sys/switch/rwlock.rs index a59944482e9..e81c854d5fb 100644 --- a/library/std/src/sys/switch/rwlock.rs +++ b/library/std/src/sys/switch/rwlock.rs @@ -4,6 +4,8 @@ pub struct RWLock { mode: UnsafeCell, } +pub type MovableRWLock = Box; + unsafe impl Send for RWLock {} unsafe impl Sync for RWLock {} // no threads on wasm diff --git a/library/std/src/sys/switch/thread.rs b/library/std/src/sys/switch/thread.rs index db08d77b632..2a160c4a970 100644 --- a/library/std/src/sys/switch/thread.rs +++ b/library/std/src/sys/switch/thread.rs @@ -5,6 +5,8 @@ use crate::mem; use crate::ptr; use crate::sys::os; use crate::time::Duration; +use crate::num::NonZeroUsize; +use crate::sys::unsupported; use nnsdk::{os::SleepThread, TimeSpan}; @@ -146,4 +148,8 @@ pub mod guard { fn min_stack_size(_: *const libc::pthread_attr_t) -> usize { 0x1000 // just a guess +} + +pub fn available_concurrency() -> io::Result { + unsupported() } \ No newline at end of file