-
Notifications
You must be signed in to change notification settings - Fork 13.8k
std: Add Motor OS std library port #147000
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
//! Motor OS-specific extensions to primitives in the [`std::ffi`] module. | ||
|
||
#![stable(feature = "rust1", since = "1.0.0")] | ||
|
||
use crate::ffi::{OsStr, OsString}; | ||
use crate::sealed::Sealed; | ||
|
||
/// Motor OS-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 { | ||
/// Motor OS strings are utf-8, and thus just strings. | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
fn as_str(&self) -> &str; | ||
} | ||
|
||
#[stable(feature = "rust1", since = "1.0.0")] | ||
impl OsStringExt for OsString { | ||
#[inline] | ||
fn as_str(&self) -> &str { | ||
self.to_str().unwrap() | ||
} | ||
} | ||
|
||
/// Motor OS-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 OsStrExt: Sealed { | ||
/// Motor OS strings are utf-8, and thus just strings. | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
fn as_str(&self) -> &str; | ||
} | ||
|
||
#[stable(feature = "rust1", since = "1.0.0")] | ||
impl OsStrExt for OsStr { | ||
#[inline] | ||
fn as_str(&self) -> &str { | ||
self.to_str().unwrap() | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#![unstable(feature = "motor_ext", issue = "none")] | ||
|
||
#[unstable(feature = "motor_ext", issue = "none")] | ||
pub fn map_motor_error(err: moto_rt::ErrorCode) -> crate::io::Error { | ||
Comment on lines
+3
to
+4
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably doesn't need to be crate public? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If a Rust program calls into moto-sys (the Motor OS kernel API) or moto-rt (the Motor OS runtime API), they get I prefer to avoid code duplication and just export it here. If it is better to duplicate it elsewhere, I can do that also, let me know. |
||
use moto_rt::error::*; | ||
|
||
use crate::io::ErrorKind; | ||
|
||
let kind: ErrorKind = match err { | ||
E_ALREADY_IN_USE => ErrorKind::AlreadyExists, | ||
E_INVALID_FILENAME => ErrorKind::InvalidFilename, | ||
E_NOT_FOUND => ErrorKind::NotFound, | ||
E_TIMED_OUT => ErrorKind::TimedOut, | ||
E_NOT_IMPLEMENTED => ErrorKind::Unsupported, | ||
E_FILE_TOO_LARGE => ErrorKind::FileTooLarge, | ||
E_UNEXPECTED_EOF => ErrorKind::UnexpectedEof, | ||
E_INVALID_ARGUMENT => ErrorKind::InvalidInput, | ||
E_NOT_READY => ErrorKind::WouldBlock, | ||
E_NOT_CONNECTED => ErrorKind::NotConnected, | ||
_ => ErrorKind::Other, | ||
}; | ||
|
||
crate::io::Error::from(kind) | ||
} | ||
|
||
pub mod ffi; | ||
pub mod process; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use crate::sealed::Sealed; | ||
use crate::sys_common::AsInner; | ||
|
||
#[unstable(feature = "motor_ext", issue = "none")] | ||
pub trait ChildExt: Sealed { | ||
/// Extracts the main thread raw handle, without taking ownership | ||
#[unstable(feature = "motor_ext", issue = "none")] | ||
fn sys_handle(&self) -> u64; | ||
} | ||
|
||
#[unstable(feature = "motor_ext", issue = "none")] | ||
impl ChildExt for crate::process::Child { | ||
fn sys_handle(&self) -> u64 { | ||
self.as_inner().handle() | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use crate::alloc::{GlobalAlloc, Layout, System}; | ||
|
||
#[stable(feature = "alloc_system_type", since = "1.28.0")] | ||
unsafe impl GlobalAlloc for System { | ||
#[inline] | ||
unsafe fn alloc(&self, layout: Layout) -> *mut u8 { | ||
// Safety: same requirements as in GlobalAlloc::alloc. | ||
moto_rt::alloc::alloc(layout) | ||
} | ||
|
||
#[inline] | ||
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 { | ||
// Safety: same requirements as in GlobalAlloc::alloc_zeroed. | ||
moto_rt::alloc::alloc_zeroed(layout) | ||
} | ||
|
||
#[inline] | ||
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { | ||
// Safety: same requirements as in GlobalAlloc::dealloc. | ||
unsafe { moto_rt::alloc::dealloc(ptr, layout) } | ||
} | ||
|
||
#[inline] | ||
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 { | ||
// Safety: same requirements as in GlobalAlloc::realloc. | ||
unsafe { moto_rt::alloc::realloc(ptr, layout, new_size) } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use crate::io; | ||
use crate::sys::fd::FileDesc; | ||
use crate::sys::pipe::anon_pipe; | ||
use crate::sys_common::IntoInner; | ||
|
||
pub type AnonPipe = FileDesc; | ||
|
||
#[inline] | ||
pub fn pipe() -> io::Result<(AnonPipe, AnonPipe)> { | ||
anon_pipe().map(|(rx, wx)| (rx.into_inner(), wx.into_inner())) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
pub use super::common::Args; | ||
use crate::ffi::OsString; | ||
|
||
pub fn args() -> Args { | ||
let motor_args: Vec<String> = moto_rt::process::args(); | ||
let mut rust_args = alloc::vec::Vec::new(); | ||
|
||
for arg in motor_args { | ||
rust_args.push(OsString::from(arg)); | ||
} | ||
|
||
Args::new(rust_args) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
pub use super::common::Env; | ||
use crate::ffi::{OsStr, OsString}; | ||
use crate::io; | ||
use crate::os::motor::ffi::OsStrExt; | ||
|
||
pub fn env() -> Env { | ||
let motor_env: Vec<(String, String)> = moto_rt::process::env(); | ||
let mut rust_env = vec![]; | ||
|
||
for (k, v) in motor_env { | ||
rust_env.push((OsString::from(k), OsString::from(v))); | ||
} | ||
|
||
Env::new(rust_env) | ||
} | ||
|
||
pub fn getenv(key: &OsStr) -> Option<OsString> { | ||
moto_rt::process::getenv(key.as_str()).map(|s| OsString::from(s)) | ||
} | ||
|
||
pub unsafe fn setenv(key: &OsStr, val: &OsStr) -> io::Result<()> { | ||
Ok(moto_rt::process::setenv(key.as_str(), val.as_str())) | ||
} | ||
|
||
pub unsafe fn unsetenv(key: &OsStr) -> io::Result<()> { | ||
Ok(moto_rt::process::unsetenv(key.as_str())) | ||
} |
Uh oh!
There was an error while loading. Please reload this page.