-
Notifications
You must be signed in to change notification settings - Fork 13.9k
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
//! Motor OS-specific extensions to primitives in the [`std::ffi`] module. | ||
#![unstable(feature = "motor_ext", issue = "147456")] | ||
|
||
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. | ||
pub trait OsStringExt: Sealed { | ||
/// Motor OS strings are utf-8, and thus just strings. | ||
fn as_str(&self) -> &str; | ||
} | ||
|
||
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. | ||
pub trait OsStrExt: Sealed { | ||
/// Motor OS strings are utf-8, and thus just strings. | ||
fn as_str(&self) -> &str; | ||
} | ||
|
||
impl OsStrExt for OsStr { | ||
#[inline] | ||
fn as_str(&self) -> &str { | ||
self.to_str().unwrap() | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#![unstable(feature = "motor_ext", issue = "147456")] | ||
|
||
pub mod ffi; | ||
pub mod process; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#![unstable(feature = "motor_ext", issue = "147456")] | ||
|
||
use crate::sealed::Sealed; | ||
use crate::sys_common::AsInner; | ||
|
||
pub trait ChildExt: Sealed { | ||
/// Extracts the main thread raw handle, without taking ownership | ||
fn sys_handle(&self) -> u64; | ||
} | ||
|
||
impl ChildExt for crate::process::Child { | ||
fn sys_handle(&self) -> u64 { | ||
self.as_inner().handle() | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) } | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = Vec::new(); | ||
|
||
for arg in motor_args { | ||
rust_args.push(OsString::from(arg)); | ||
} | ||
|
||
Args::new(rust_args) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If Motor OS strings are guaranteed UTF-8, why not do
str::from_utf8_unchecked(self.as_encoded_bytes())
?Shouldn't this be
into_string
; otherwise it's redundant toOsStrExt
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But I suppose we can't rely on OS strings remaining UTF-8, since there's no UTF-8-guaranteed variant of OsString. (The user could push arbitrary bytes)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this is an argument for a UTF-8 OsString, but I'd also never heard of Motor OS, so maybe it's not worth the extra implementation cost.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, OsString currently is not platform specific in Rust, but just bytes. So while Motor OS is very much Rust-first, and Rust strings are UTF-8, there is no guarantee that bytes in OsString are actually UTF-8, so
unwrap()
seems appropriate here.Feel free to play around and post your impressions on Motor OS github (I'll need to update the building instructions doc, now that this PR is in).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wrote more about my thoughts on this in #147797