From f61ac95953e1d70aaf98e3a070b8f80e1f13cdae Mon Sep 17 00:00:00 2001 From: Japabu Date: Tue, 28 Jul 2026 09:14:12 +0200 Subject: [PATCH] Add ToyOS support MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ToyOS (https://github.com/Japabu/toyos) is an operating system written from scratch in Rust for x86-64, with `target_os = "toyos"` supported by a fork of rustc and std. Its display server is a userspace compositor daemon; clients create windows over IPC and draw into a shared-memory framebuffer. This adds `ToyOsWindowHandle` and `ToyOsDisplayHandle` following the same pattern as the existing Haiku and Orbital handles: the window handle carries a `NonNull` pointer to the client-side window object, and the display handle is empty because the compositor is reached through the per-window IPC connection rather than a shared connection object. Both are registered at every site those platforms are — module, re-export, both enums, both `from_impl!` sets, and the `Send`/`Sync` assertions. The handles are already used by ToyOS's winit and softbuffer backends. Co-Authored-By: Claude Fable 5 --- CHANGELOG.md | 1 + src/lib.rs | 16 +++++++++ src/toyos.rs | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 src/toyos.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f22ef5..15e28ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ - **Breaking:** Rename `Web*` handles to `WasmBindgen*`, to make it clearer that these are specific to `wasm-bindgen`. - Enabled `std` feature by default. - Add owned window and display handles. +- Add ToyOS support. (#223) - Improve documentation on AppKit and UIKit handles. ## 0.6.2 (2024-05-17) diff --git a/src/lib.rs b/src/lib.rs index c09f64f..7b93b36 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -49,6 +49,7 @@ mod haiku; mod ohos; mod owned; mod redox; +mod toyos; mod uikit; mod wayland; mod web; @@ -67,6 +68,7 @@ pub use owned::{ SyncWindowHandle, WindowHandleVtable, }; pub use redox::{OrbitalDisplayHandle, OrbitalWindowHandle}; +pub use toyos::{ToyOsDisplayHandle, ToyOsWindowHandle}; pub use uikit::{UiKitDisplayHandle, UiKitWindowHandle}; pub use wayland::{WaylandDisplayHandle, WaylandWindowHandle}; pub use web::{ @@ -200,6 +202,11 @@ pub enum RawWindowHandle { /// ## Availability Hints /// This variant is used on HaikuOS. Haiku(HaikuWindowHandle), + /// A raw window handle for ToyOS. + /// + /// ## Availability Hints + /// This variant is used on ToyOS (`target_os = "toyos"`). + ToyOs(ToyOsWindowHandle), } /// A display server handle for a particular windowing system. @@ -313,6 +320,11 @@ pub enum RawDisplayHandle { /// ## Availability Hints /// This variant is used on HaikuOS. Haiku(HaikuDisplayHandle), + /// A raw display handle for ToyOS. + /// + /// ## Availability Hints + /// This variant is used on ToyOS (`target_os = "toyos"`). + ToyOs(ToyOsDisplayHandle), } /// An error that can occur while fetching a display or window handle. @@ -384,6 +396,7 @@ from_impl!(RawDisplayHandle, Windows, WindowsDisplayHandle); from_impl!(RawDisplayHandle, WasmBindgen, WasmBindgenDisplay); from_impl!(RawDisplayHandle, Android, AndroidDisplayHandle); from_impl!(RawDisplayHandle, Haiku, HaikuDisplayHandle); +from_impl!(RawDisplayHandle, ToyOs, ToyOsDisplayHandle); from_impl!(RawWindowHandle, UiKit, UiKitWindowHandle); from_impl!(RawWindowHandle, AppKit, AppKitWindowHandle); @@ -408,6 +421,7 @@ from_impl!( ); from_impl!(RawWindowHandle, AndroidNdk, AndroidNdkWindowHandle); from_impl!(RawWindowHandle, Haiku, HaikuWindowHandle); +from_impl!(RawWindowHandle, ToyOs, ToyOsWindowHandle); #[cfg(test)] mod tests { @@ -443,6 +457,7 @@ mod tests { assert_not_impl_any!(WasmBindgenDisplay: Send, Sync); assert_impl_all!(AndroidDisplayHandle: Send, Sync); assert_impl_all!(HaikuDisplayHandle: Send, Sync); + assert_impl_all!(ToyOsDisplayHandle: Send, Sync); // TODO: Unsure if some of these should not actually be Send + Sync assert_not_impl_any!(UiKitWindowHandle: Send, Sync); @@ -462,6 +477,7 @@ mod tests { assert_not_impl_any!(WasmBindgenOffscreenCanvasWindowHandle: Send, Sync); assert_impl_all!(AndroidNdkWindowHandle: Send, Sync); assert_impl_all!(HaikuWindowHandle: Send, Sync); + assert_impl_all!(ToyOsWindowHandle: Send, Sync); assert_impl_all!(OwnedDisplayHandle: Unpin, UnwindSafe, RefUnwindSafe); assert_not_impl_any!(OwnedDisplayHandle: Send, Sync); diff --git a/src/toyos.rs b/src/toyos.rs new file mode 100644 index 0000000..e64c784 --- /dev/null +++ b/src/toyos.rs @@ -0,0 +1,92 @@ +use core::ffi::c_void; +use core::ptr::NonNull; + +use super::DisplayHandle; + +/// Raw display handle for ToyOS. +/// +/// ## Thread Safety +/// +/// The display is the ToyOS compositor, which clients reach through a per-window +/// IPC connection rather than through a shared connection object. Therefore this +/// type is `Send` and `Sync`. +/// +/// Note that this type does not currently contain any ToyOS objects. This type +/// is kept as `Send` and `Sync` in preparation for such objects to be added to +/// this type. +#[non_exhaustive] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct ToyOsDisplayHandle {} + +impl ToyOsDisplayHandle { + /// Create a new empty display handle. + /// + /// + /// # Example + /// + /// ``` + /// # use raw_window_handle::ToyOsDisplayHandle; + /// let handle = ToyOsDisplayHandle::new(); + /// ``` + pub fn new() -> Self { + Self {} + } +} + +impl DisplayHandle<'static> { + /// Create a ToyOS-based display handle. + /// + /// As no data is borrowed by this handle, it is completely safe to create. This function + /// may be useful to windowing framework implementations that want to avoid unsafe code. + /// + /// # Example + /// + /// ``` + /// # use raw_window_handle::{DisplayHandle, HasDisplayHandle}; + /// # fn do_something(rwh: impl HasDisplayHandle) { let _ = rwh; } + /// let handle = DisplayHandle::toyos(); + /// do_something(handle); + /// ``` + pub fn toyos() -> Self { + // SAFETY: No data is borrowed. + unsafe { Self::borrow_raw(ToyOsDisplayHandle::new().into()) } + } +} + +/// Raw window handle for ToyOS. +/// +/// ## Thread Safety +/// +/// The underlying window owns an IPC connection to the compositor and the +/// shared-memory framebuffer behind it, and calls on the window correspond to +/// reads and writes through that connection. Therefore this type is `Send` and +/// `Sync`. This means that this type can be sent to and used from other threads. +#[non_exhaustive] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct ToyOsWindowHandle { + /// A pointer to a ToyOS window object. + pub window: NonNull, +} + +unsafe impl Send for ToyOsWindowHandle {} +unsafe impl Sync for ToyOsWindowHandle {} + +impl ToyOsWindowHandle { + /// Create a new handle to a window. + /// + /// + /// # Example + /// + /// ``` + /// # use core::ptr::NonNull; + /// # use raw_window_handle::ToyOsWindowHandle; + /// # type Window = (); + /// # + /// let window: NonNull; + /// # window = NonNull::from(&()); + /// let mut handle = ToyOsWindowHandle::new(window.cast()); + /// ``` + pub fn new(window: NonNull) -> Self { + Self { window } + } +}