Skip to content
This repository was archived by the owner on May 24, 2022. It is now read-only.

Commit c5c76ea

Browse files
authored
Merge pull request #3 from Raytwo/fix-switch-build
Add missing functions for the Nintendo Switch
2 parents 1f88570 + e447fd6 commit c5c76ea

File tree

6 files changed

+123
-7
lines changed

6 files changed

+123
-7
lines changed

library/std/src/os/switch.rs

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//! Unix-specific extension to the primitives in the `std::ffi` module.
2+
//!
3+
//! # Examples
4+
//!
5+
//! ```
6+
//! use std::ffi::OsString;
7+
//! use std::os::unix::ffi::OsStringExt;
8+
//!
9+
//! let bytes = b"foo".to_vec();
10+
//!
11+
//! // OsStringExt::from_vec
12+
//! let os_string = OsString::from_vec(bytes);
13+
//! assert_eq!(os_string.to_str(), Some("foo"));
14+
//!
15+
//! // OsStringExt::into_vec
16+
//! let bytes = os_string.into_vec();
17+
//! assert_eq!(bytes, b"foo");
18+
//! ```
19+
//!
20+
//! ```
21+
//! use std::ffi::OsStr;
22+
//! use std::os::unix::ffi::OsStrExt;
23+
//!
24+
//! let bytes = b"foo";
25+
//!
26+
//! // OsStrExt::from_bytes
27+
//! let os_str = OsStr::from_bytes(bytes);
28+
//! assert_eq!(os_str.to_str(), Some("foo"));
29+
//!
30+
//! // OsStrExt::as_bytes
31+
//! let bytes = os_str.as_bytes();
32+
//! assert_eq!(bytes, b"foo");
33+
//! ```
34+
35+
#![stable(feature = "rust1", since = "1.0.0")]
36+
mod os_str;
37+
38+
#[stable(feature = "rust1", since = "1.0.0")]
39+
pub use self::os_str::{OsStrExt, OsStringExt};
40+
41+
#[stable(feature = "rust1", since = "1.0.0")]
42+
mod ffi {
43+
#[stable(feature = "rust1", since = "1.0.0")]
44+
pub use crate::sys_common::os_str_bytes::*;
45+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
use crate::ffi::{OsStr, OsString};
2+
use crate::mem;
3+
use crate::sealed::Sealed;
4+
use crate::sys::os_str::Buf;
5+
use crate::sys_common::{AsInner, FromInner, IntoInner};
6+
7+
// Note: this file is currently reused in other `std::os::{platform}::ffi` modules to reduce duplication.
8+
// Keep this in mind when applying changes to this file that only apply to `unix`.
9+
10+
/// Platform-specific extensions to [`OsString`].
11+
///
12+
/// This trait is sealed: it cannot be implemented outside the standard library.
13+
/// This is so that future additional methods are not breaking changes.
14+
#[stable(feature = "rust1", since = "1.0.0")]
15+
pub trait OsStringExt: Sealed {
16+
/// Creates an [`OsString`] from a byte vector.
17+
///
18+
/// See the module documentation for an example.
19+
#[stable(feature = "rust1", since = "1.0.0")]
20+
fn from_vec(vec: Vec<u8>) -> Self;
21+
22+
/// Yields the underlying byte vector of this [`OsString`].
23+
///
24+
/// See the module documentation for an example.
25+
#[stable(feature = "rust1", since = "1.0.0")]
26+
fn into_vec(self) -> Vec<u8>;
27+
}
28+
29+
#[stable(feature = "rust1", since = "1.0.0")]
30+
impl OsStringExt for OsString {
31+
fn from_vec(vec: Vec<u8>) -> OsString {
32+
FromInner::from_inner(Buf { inner: vec })
33+
}
34+
fn into_vec(self) -> Vec<u8> {
35+
self.into_inner().inner
36+
}
37+
}
38+
39+
/// Platform-specific extensions to [`OsStr`].
40+
///
41+
/// This trait is sealed: it cannot be implemented outside the standard library.
42+
/// This is so that future additional methods are not breaking changes.
43+
#[stable(feature = "rust1", since = "1.0.0")]
44+
pub trait OsStrExt: Sealed {
45+
#[stable(feature = "rust1", since = "1.0.0")]
46+
/// Creates an [`OsStr`] from a byte slice.
47+
///
48+
/// See the module documentation for an example.
49+
fn from_bytes(slice: &[u8]) -> &Self;
50+
51+
/// Gets the underlying byte view of the [`OsStr`] slice.
52+
///
53+
/// See the module documentation for an example.
54+
#[stable(feature = "rust1", since = "1.0.0")]
55+
fn as_bytes(&self) -> &[u8];
56+
}
57+
58+
#[stable(feature = "rust1", since = "1.0.0")]
59+
impl OsStrExt for OsStr {
60+
#[inline]
61+
fn from_bytes(slice: &[u8]) -> &OsStr {
62+
unsafe { mem::transmute(slice) }
63+
}
64+
#[inline]
65+
fn as_bytes(&self) -> &[u8] {
66+
&self.as_inner().inner
67+
}
68+
}

library/std/src/os/switch/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#![stable(feature = "rust1", since = "1.0.0")]
2+
pub mod ffi;

library/std/src/sys/switch/rwlock.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ pub struct RWLock {
44
mode: UnsafeCell<isize>,
55
}
66

7+
pub type MovableRWLock = Box<RWLock>;
8+
79
unsafe impl Send for RWLock {}
810
unsafe impl Sync for RWLock {} // no threads on wasm
911

library/std/src/sys/switch/thread.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use crate::mem;
55
use crate::ptr;
66
use crate::sys::os;
77
use crate::time::Duration;
8+
use crate::num::NonZeroUsize;
9+
use crate::sys::unsupported;
810

911
use nnsdk::{os::SleepThread, TimeSpan};
1012

@@ -146,4 +148,8 @@ pub mod guard {
146148

147149
fn min_stack_size(_: *const libc::pthread_attr_t) -> usize {
148150
0x1000 // just a guess
151+
}
152+
153+
pub fn available_concurrency() -> io::Result<NonZeroUsize> {
154+
unsupported()
149155
}

0 commit comments

Comments
 (0)