Skip to content

Commit

Permalink
Add no_std compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleMayes committed Sep 23, 2023
1 parent b114894 commit a8d3e80
Show file tree
Hide file tree
Showing 36 changed files with 201 additions and 103 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ jobs:
with:
command: test
args: --verbose --all-features
# Test (no_std)
- name: Cargo Build (vulkanalia-sys/no_std)
uses: actions-rs/cargo@v1
with:
command: build
args: --manifest-path vulkanalia-sys/Cargo.toml --no-default-features
- name: Cargo Build (vulkanalia/no_std)
uses: actions-rs/cargo@v1
with:
command: build
args: --manifest-path vulkanalia/Cargo.toml --no-default-features

format-vulkanalia:
name: Format - Vulkanalia
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## [0.23.0] - UNRELEASED

### Changed
- Added `no_std` compability for `vulkanalia` and `vulkanalia-sys` crates

## [0.22.0] - 2023-09-15

### Fixed
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@ For users new to Vulkan, there is a complete adaptation of https://vulkan-tutori

## Cargo Features

The `vulkanalia` crate has the following notable Cargo features:
The `vulkanalia` crate has the following notable non-default Cargo features:

* `libloading` (**non-default**) – enables integration with [`libloading`](https://crates.io/crates/libloading) (adds the [`LibloadingLoader`](https://docs.rs/vulkanalia/latest/vulkanalia/loader/struct.LibloadingLoader.html) struct which can be used to load the initial Vulkan commands from a Vulkan shared library)
* `window` (**non-default**) – enables integration with [`raw-window-handle`](https://crates.io/crates/raw-window-handle) (adds the [`window`](https://docs.rs/vulkanalia/latest/vulkanalia/window/index.html) module which can be used to create surfaces for windows from libraries that support `raw-window-handle` (e.g., [`winit`](https://crates.io/crates/winit))
* `provisional` (**non-default**) – enables access to [provisional Vulkan extensions](https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/provisional-headers.html) (**WARNING:** these extensions are not guaranteed to be backwards compatible and are not intended to be used in production applications)

By default, the `vulkanalia-sys` and `vulkanalia` crates depend on the Rust standard library. However, by disabling the default features for these crates, you can use either of these crates in a `no_std` environment. If you do this, the following features are of note:

* `no_std_error` (**non-default**): enables implementations of the [`Error` trait](https://doc.rust-lang.org/beta/core/error/trait.Error.html) for various error types in `vulkanalia` and `vulkanalia-sys` when the default `std` feature is not enabled (the usage of the `Error` trait in `core` is [not yet stable](https://github.com/rust-lang/rust/issues/103765) and requires the `core-error` feature to be enabled)

## Example

See the `examples` directory for an implementation of the classic triangle example using `vulkanalia`.
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ import com.kylemayes.generator.support.PeekableIterator
/** Generates Rust structs to build Vulkan structs. */
fun Registry.generateBuilders() =
"""
use std::fmt;
use std::marker::PhantomData;
use std::ops;
use std::os::raw::{c_char, c_int, c_void};
use std::ptr::NonNull;
use core::ffi::{c_char, c_int, c_void};
use core::fmt;
use core::marker::PhantomData;
use core::ops;
use core::ptr::NonNull;
use super::*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import java.lang.Error

fun Registry.generateChains(): String {
return """
use std::os::raw::c_void;
use core::ffi::c_void;
use super::*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import com.kylemayes.generator.registry.Registry
/** Generates Rust type aliases for Vulkan commands. */
fun Registry.generateCommands() =
"""
use std::os::raw::{c_char, c_int, c_void};
use core::ffi::{c_char, c_int, c_void};
use crate::*;
Expand Down Expand Up @@ -41,8 +41,8 @@ fun Registry.generateCommandStructs(): String {
generateCommandStruct(it.key, supported)
}
return """
use std::mem;
use std::os::raw::{c_char, c_int, c_void};
use core::mem;
use core::ffi::{c_char, c_int, c_void};
use super::*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ import com.kylemayes.generator.registry.intern
/** Generates Rust structs for Vulkan enums. */
fun Registry.generateEnums() =
"""
use core::fmt;
#[cfg(feature = "std")]
use std::error;
use std::fmt;
#[cfg(all(feature = "no_std_error", not(feature="std")))]
use core::error;
${enums.values.sortedBy { it.name }.joinToString("\n") { generateEnum(it) }}
${generateAliases(enums.keys)}
Expand All @@ -22,8 +26,12 @@ ${generateAliases(enums.keys)}
/** Generates Rust structs for success result and error result enums. */
fun Registry.generateResultEnums() =
"""
use core::fmt;
#[cfg(feature = "std")]
use std::error;
use std::fmt;
#[cfg(all(feature = "no_std_error", not(feature="std")))]
use core::error;
use super::Result;
Expand Down Expand Up @@ -68,7 +76,7 @@ private fun Registry.generateEnum(enum: Enum, documentation: String? = null): St
val (display, error) = if (enum.name.value == "Result" || enum.name.value == "ErrorCode") {
val default = "write!(f, \"unknown Vulkan result (code = {})\", self.0)"
val display = generateFmtImpl(enum, "Display", default) { "\"${results[it.value] ?: it.name.value}\"" }
display to "impl error::Error for ${enum.name} { }"
display to "#[cfg(any(feature=\"std\", feature=\"no_std_error\"))] impl error::Error for ${enum.name} { }"
} else {
"" to ""
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,10 @@ pub const ${extension.name}_EXTENSION: Extension = Extension {
/** Generates Rust modules and traits for Vulkan extensions. */
fun Registry.generateExtensionTraits() =
"""
use std::mem::MaybeUninit;
use std::os::raw::{c_int, c_void};
use std::ptr;
use alloc::vec::Vec;
use core::ffi::{c_int, c_void};
use core::mem::MaybeUninit;
use core::ptr;
use super::*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import com.kylemayes.generator.registry.Registry
/** Generates Rust type aliases for Vulkan function pointer types. */
fun Registry.generateFunctions() =
"""
use std::os::raw::{c_char, c_void};
use core::ffi::{c_char, c_void};
use crate::*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import com.kylemayes.generator.support.toSnakeCase
/** Generates Rust structs for Vulkan handles. */
fun Registry.generateHandles() =
"""
use std::fmt;
use std::hash::Hash;
use core::fmt;
use core::hash::Hash;
use crate::ObjectType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import com.kylemayes.generator.support.PeekableIterator
/** Generates Rust structs for Vulkan structs. */
fun Registry.generateStructs(): String {
return """
use std::fmt;
use std::os::raw::{c_char, c_int, c_void};
use std::ptr;
use core::ffi::{c_char, c_int, c_void};
use core::fmt;
use core::ptr;
use crate::*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import com.kylemayes.generator.registry.Typedef
/** Generates Rust type aliases for Vulkan typedefs and platform types. */
fun Registry.generateTypedefs() =
"""
use std::os::raw::{c_ulong, c_void};
use core::ffi::{c_ulong, c_void};
${basetypes.values.sortedBy { it.name }.joinToString("\n") { generateTypedef(it) }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import com.kylemayes.generator.registry.Structure
/** Generates Rust unions for Vulkan unions. */
fun Registry.generateUnions() =
"""
use std::fmt;
use std::mem::MaybeUninit;
use std::os::raw::{c_char, c_void};
use core::ffi::{c_char, c_void};
use core::fmt;
use core::mem::MaybeUninit;
use crate::*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import com.kylemayes.generator.registry.Version
fun Registry.generateVersionTraits(): String {
var versions =
"""
use std::mem::MaybeUninit;
use std::os::raw::c_void;
use std::ptr;
use alloc::vec::Vec;
use core::ffi::c_void;
use core::mem::MaybeUninit;
use core::ptr;
use super::*;
"""
Expand Down
5 changes: 5 additions & 0 deletions vulkanalia-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ categories = ["graphics"]

[features]

default = ["std"]

std = []
no_std_error = []

provisional = []

[dependencies]
Expand Down
14 changes: 7 additions & 7 deletions vulkanalia-sys/src/arrays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
clippy::upper_case_acronyms
)]

use std::borrow::Cow;
use std::ffi::CStr;
use std::fmt;
use std::hash;
use std::ops;
use std::os::raw::c_char;
use alloc::borrow::Cow;
use alloc::string::String;
use core::ffi::{c_char, CStr};
use core::fmt;
use core::hash;
use core::ops;

/// An array containing a sequence of bytes.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
Expand Down Expand Up @@ -88,7 +88,7 @@ impl<const N: usize> From<ByteArray<N>> for [u8; N] {
///
/// assert_eq!(hasher1.finish(), hasher2.finish());
/// ```
#[derive(Copy, Clone)]
#[derive(Copy, Clone, PartialOrd, Ord)]
#[repr(transparent)]
pub struct StringArray<const N: usize>([c_char; N]);

Expand Down
2 changes: 1 addition & 1 deletion vulkanalia-sys/src/bitfields.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: Apache-2.0

use std::fmt;
use core::fmt;

/// A pair of bitfields stored in an `u32`.
///
Expand Down
2 changes: 1 addition & 1 deletion vulkanalia-sys/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
clippy::useless_transmute
)]

use std::os::raw::{c_char, c_int, c_void};
use core::ffi::{c_char, c_int, c_void};

use crate::*;

Expand Down
7 changes: 6 additions & 1 deletion vulkanalia-sys/src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@
clippy::useless_transmute
)]

use core::fmt;

#[cfg(all(feature = "no_std_error", not(feature = "std")))]
use core::error;
#[cfg(feature = "std")]
use std::error;
use std::fmt;

/// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/VkAccelerationStructureBuildTypeKHR.html>
#[repr(transparent)]
Expand Down Expand Up @@ -4545,6 +4549,7 @@ impl fmt::Display for Result {
}
}

#[cfg(any(feature = "std", feature = "no_std_error"))]
impl error::Error for Result {}

/// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/VkSamplerAddressMode.html>
Expand Down
2 changes: 1 addition & 1 deletion vulkanalia-sys/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
clippy::useless_transmute
)]

use std::os::raw::{c_char, c_void};
use core::ffi::{c_char, c_void};

use crate::*;

Expand Down
4 changes: 2 additions & 2 deletions vulkanalia-sys/src/handles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
clippy::useless_transmute
)]

use std::fmt;
use std::hash::Hash;
use core::fmt;
use core::hash::Hash;

use crate::ObjectType;

Expand Down
5 changes: 5 additions & 0 deletions vulkanalia-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

//! Raw Vulkan bindings for Rust.

#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(feature = "core_error", feature(no_std_error))]

extern crate alloc;

mod arrays;
mod bitfields;

Expand Down
6 changes: 3 additions & 3 deletions vulkanalia-sys/src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
clippy::useless_transmute
)]

use std::fmt;
use std::os::raw::{c_char, c_int, c_void};
use std::ptr;
use core::ffi::{c_char, c_int, c_void};
use core::fmt;
use core::ptr;

use crate::*;

Expand Down
2 changes: 1 addition & 1 deletion vulkanalia-sys/src/typedefs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
clippy::useless_transmute
)]

use std::os::raw::{c_ulong, c_void};
use core::ffi::{c_ulong, c_void};

/// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/VkBool32.html>
pub type Bool32 = u32;
Expand Down
6 changes: 3 additions & 3 deletions vulkanalia-sys/src/unions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
clippy::useless_transmute
)]

use std::fmt;
use std::mem::MaybeUninit;
use std::os::raw::{c_char, c_void};
use core::ffi::{c_char, c_void};
use core::fmt;
use core::mem::MaybeUninit;

use crate::*;

Expand Down
7 changes: 6 additions & 1 deletion vulkanalia/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,19 @@ categories = ["graphics"]

[features]

default = ["std"]

std = ["vulkanalia-sys/std"]
no_std_error = ["vulkanalia-sys/no_std_error"]

provisional = ["vulkanalia-sys/provisional"]
window = ["raw-window-handle", "cocoa", "metal", "objc"]

[dependencies]

libloading = { version = "0.7", optional = true }
raw-window-handle = { version = "0.5", optional = true }
vulkanalia-sys = { version = "0.22", path = "../vulkanalia-sys" }
vulkanalia-sys = { version = "0.22", path = "../vulkanalia-sys", default-features = false }

[target.'cfg(target_os = "macos")'.dependencies]

Expand Down
Loading

0 comments on commit a8d3e80

Please sign in to comment.