std/
lib.rs

1#![no_std]
2
3//! Welcome to stdworld! This crate is an alternative to the Rust std crate,
4//! with a focus on use-cases not supported by Rust.
5//!
6//! # Motivation: Issues with std
7//!
8//! While Rust works fine for a lot of use-cases, there are some use-cases
9//! that Rust either neglects, such as code/library unloading, or is actively
10//! hostile against, such as signal handling.
11//!
12//! This crate provides a library-only approach that addresses those use-cases.
13//!
14//! # Structure
15//!
16//! This crate is structured in modules analogous to Rust's own std. However,
17//! these are explicitly **not** drop-in replacements! Many of them differ in
18//! safety (often with the stdworld equivalent being safe), but all of them
19//! differ in types.
20//!
21//! # Stability
22//!
23//! This crate is currently unusable.
24//!
25//! You can look at the code tho.
26//!
27//! # Acknowledgements
28//!
29//! This crate was inspired by crates such as `qcell` and our own `selfref`. We
30//! would also like to thank the various trans developers. Finally, we would
31//! like to blame tom7, for getting his PhD in 2008.
32
33// TODO find a way to enforce no_std in user crates. find a way to enforce std
34// is not in the user crate sysroot.
35// "rust is safe" our ass. if it were safe this crate wouldn't fucking exist.
36// oh well.
37
38pub mod os {
39    pub struct OsStr {
40        _tbd: (),
41    }
42
43    pub struct OsString {
44        _tbd: (),
45    }
46}
47
48/// APIs to interact with signal handling.
49pub mod signal {
50    // to start off, we can always expose the C standard safe functions, and,
51    // on POSIX, also the POSIX standard safe functions.
52    // but then we have 2 ways to handle signal-unsafe functions:
53    // the C way, where none of them are available.
54    // OR
55    // the POSIX way, where the entire thing is extremely context-sensitive.
56    // the POSIX way is less restrictive, but much much harder to encode in
57    // the type system. especially as we have to support the C way regardless,
58    // since we're not targeting POSIX-only here.
59    // unfortunately we currently do not know if we can fulfill both signal
60    // safety styles in a single std replacement crate.
61}
62
63/// APIs to interact with the program environment.
64pub mod env {
65    use crate::os::{OsStr, OsString};
66
67    pub struct VarsOs {
68        _tbd: ()
69    }
70
71    /// Sets an environment variable.
72    pub fn set_var<W: EnvAccess, K: AsRef<OsStr>, V: AsRef<OsStr>>(w: W, key: K, value: V) {
73        // NYI
74    }
75    /// Fetches an environment variable.
76    pub fn var_os<W: EnvAccess, K: AsRef<OsStr>>(w: W, key: K) -> Option<OsString> {
77        // NYI
78        None
79    }
80    /// Iterates through environment variables.
81    pub fn vars_os<W: EnvAccess>(w: W) -> VarsOs {
82        VarsOs { _tbd: () }
83    }
84
85    /// Marker trait to indicate a world that is allowed to access environment
86    /// variables.
87    pub unsafe trait EnvAccess {}
88}
89
90/// APIs to interact with threads.
91pub mod thread {
92    /// Spawn a new thread.
93    pub fn spawn<W: ThreadAccess, F, T>(w: W, f: F) -> JoinHandle<T>
94    where
95        F: FnOnce(<W as ThreadAccess>::World) -> T + Send + 'static,
96        T: Send + 'static,
97    {
98        // NYI
99        JoinHandle { _t: None }
100    }
101
102    /// Marker trait to indicate a world that is allowed to spawn threads.
103    pub unsafe trait ThreadAccess {
104        /// World type used in the spawned thread.
105        type World;
106    }
107
108    pub struct JoinHandle<T> {
109        _t: Option<T>,
110    }
111}
112
113/// The world as seen by an application's main thread.
114pub struct MainWorld {
115    _tbd: (),
116}
117
118unsafe impl env::EnvAccess for MainWorld {}
119unsafe impl thread::ThreadAccess for MainWorld {
120    type World = ThreadWorld;
121}
122
123/// The world as seen by an application's other threads.
124pub struct ThreadWorld {
125    _tbd: (),
126}
127
128unsafe impl thread::ThreadAccess for ThreadWorld {
129    type World = ThreadWorld;
130}
131
132/// The world as seen by an unloadable plugin running on an application's main
133/// thread.
134pub struct UnloadableMainWorld {
135    _tbd: (),
136}
137
138unsafe impl env::EnvAccess for UnloadableMainWorld {}
139
140impl MainWorld {
141    unsafe fn new() -> MainWorld {
142        MainWorld { _tbd: () }
143    }
144}
145
146impl ThreadWorld {
147    unsafe fn new() -> ThreadWorld {
148        ThreadWorld { _tbd: () }
149    }
150}
151
152impl UnloadableMainWorld {
153    unsafe fn new() -> UnloadableMainWorld {
154        UnloadableMainWorld { _tbd: () }
155    }
156}