Skip to content
This repository was archived by the owner on May 24, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions library/std/src/os/switch.rs

This file was deleted.

45 changes: 45 additions & 0 deletions library/std/src/os/switch/ffi/mod.rs
Original file line number Diff line number Diff line change
@@ -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::*;
}
68 changes: 68 additions & 0 deletions library/std/src/os/switch/ffi/os_str.rs
Original file line number Diff line number Diff line change
@@ -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<u8>) -> 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<u8>;
}

#[stable(feature = "rust1", since = "1.0.0")]
impl OsStringExt for OsString {
fn from_vec(vec: Vec<u8>) -> OsString {
FromInner::from_inner(Buf { inner: vec })
}
fn into_vec(self) -> Vec<u8> {
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
}
}
2 changes: 2 additions & 0 deletions library/std/src/os/switch/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#![stable(feature = "rust1", since = "1.0.0")]
pub mod ffi;
2 changes: 2 additions & 0 deletions library/std/src/sys/switch/rwlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ pub struct RWLock {
mode: UnsafeCell<isize>,
}

pub type MovableRWLock = Box<RWLock>;

unsafe impl Send for RWLock {}
unsafe impl Sync for RWLock {} // no threads on wasm

Expand Down
6 changes: 6 additions & 0 deletions library/std/src/sys/switch/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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<NonZeroUsize> {
unsupported()
}