Skip to content

Commit

Permalink
Support no_std
Browse files Browse the repository at this point in the history
This increases the MSRV to 1.81 for `core::error`.
  • Loading branch information
waywardmonkeys committed Dec 27, 2024
1 parent 62d0847 commit 5efb018
Show file tree
Hide file tree
Showing 22 changed files with 99 additions and 51 deletions.
26 changes: 19 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ env:
# If the compilation fails, then the version specified here needs to be bumped up to reality.
# Be sure to also update the rust-version property in the workspace Cargo.toml file,
# plus all the README.md files of the affected packages.
RUST_MIN_VER: "1.65"
RUST_MIN_VER: "1.81"
# List of packages that will be checked with the minimum supported Rust version.
# This should be limited to packages that are intended for publishing.
RUST_MIN_VER_PKGS: "-p svgtypes"
# List of features that depend on the standard library and will be excluded from no_std checks.
FEATURES_DEPENDING_ON_STD: "std,default"


# Rationale
Expand Down Expand Up @@ -39,6 +41,10 @@ env:
# The MSRV jobs run only cargo check because different clippy versions can disagree on goals and
# running tests introduces dev dependencies which may require a higher MSRV than the bare package.
#
# For no_std checks we target x86_64-unknown-none, because this target doesn't support std
# and as such will error out if our dependency tree accidentally tries to use std.
# https://doc.rust-lang.org/stable/rustc/platform-support/x86_64-unknown-none.html
#
# We don't save caches in the merge-group cases, because those caches will never be re-used (apart
# from the very rare cases where there are multiple PRs in the merge queue).
# This is because GitHub doesn't share caches between merge queues and the main branch.
Expand Down Expand Up @@ -105,11 +111,14 @@ jobs:
with:
save-if: ${{ github.event_name != 'merge_group' }}

- name: cargo clippy (no_std)
run: cargo hack clippy --workspace --locked --optional-deps --each-feature --ignore-unknown-features --features libm --exclude-features ${{ env.FEATURES_DEPENDING_ON_STD }} --target x86_64-unknown-none -- -D warnings

- name: cargo clippy
run: cargo hack clippy --workspace --locked --optional-deps --each-feature -- -D warnings
run: cargo hack clippy --workspace --locked --optional-deps --each-feature --ignore-unknown-features --features std -- -D warnings

- name: cargo clippy (auxiliary)
run: cargo hack clippy --workspace --locked --optional-deps --each-feature --tests --examples -- -D warnings
run: cargo hack clippy --workspace --locked --optional-deps --each-feature --ignore-unknown-features --features std --tests --examples -- -D warnings

clippy-stable-wasm:
name: cargo clippy (wasm32)
Expand All @@ -135,10 +144,10 @@ jobs:
save-if: ${{ github.event_name != 'merge_group' }}

- name: cargo clippy
run: cargo hack clippy --workspace --locked --target wasm32-unknown-unknown --optional-deps --each-feature -- -D warnings
run: cargo hack clippy --workspace --locked --target wasm32-unknown-unknown --optional-deps --each-feature --ignore-unknown-features --features std -- -D warnings

- name: cargo clippy (auxiliary)
run: cargo hack clippy --workspace --locked --target wasm32-unknown-unknown --optional-deps --each-feature --tests --examples -- -D warnings
run: cargo hack clippy --workspace --locked --target wasm32-unknown-unknown --optional-deps --each-feature --ignore-unknown-features --features std --tests --examples -- -D warnings

test-stable:
name: cargo test
Expand Down Expand Up @@ -216,8 +225,11 @@ jobs:
with:
save-if: ${{ github.event_name != 'merge_group' }}

- name: cargo check (no_std)
run: cargo hack check ${{ env.RUST_MIN_VER_PKGS }} --locked --optional-deps --each-feature --ignore-unknown-features --features libm --exclude-features ${{ env.FEATURES_DEPENDING_ON_STD }} --target x86_64-unknown-none

- name: cargo check
run: cargo hack check ${{ env.RUST_MIN_VER_PKGS }} --locked --optional-deps --each-feature
run: cargo hack check ${{ env.RUST_MIN_VER_PKGS }} --locked --optional-deps --each-feature --ignore-unknown-features --features std

check-msrv-wasm:
name: cargo check (msrv) (wasm32)
Expand All @@ -242,7 +254,7 @@ jobs:
save-if: ${{ github.event_name != 'merge_group' }}

- name: cargo check
run: cargo hack check ${{ env.RUST_MIN_VER_PKGS }} --locked --target wasm32-unknown-unknown --optional-deps --each-feature
run: cargo hack check ${{ env.RUST_MIN_VER_PKGS }} --locked --target wasm32-unknown-unknown --optional-deps --each-feature --ignore-unknown-features --features std

doc:
name: cargo doc
Expand Down
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ exclude = ["benches/", "codegen/", "fuzz/"]
[workspace]
members = ["benches"]

[features]
default = ["std"]
std = ["kurbo/std"]
libm = ["kurbo/libm"]

[dependencies]
siphasher = "1.0" # perfect hash implementation for color names
kurbo = "0.11" # ArcTo to CurveTo(s)
kurbo = { version = "0.11", default-features = false } # ArcTo to CurveTo(s)
4 changes: 2 additions & 2 deletions src/angle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl Angle {
}
}

impl std::str::FromStr for Angle {
impl core::str::FromStr for Angle {
type Err = Error;

#[inline]
Expand Down Expand Up @@ -99,7 +99,7 @@ impl Stream<'_> {
#[cfg(test)]
mod tests {
use super::*;
use std::str::FromStr;
use core::str::FromStr;

macro_rules! test_p {
($name:ident, $text:expr, $result:expr) => (
Expand Down
4 changes: 2 additions & 2 deletions src/aspect_ratio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub struct AspectRatio {
pub slice: bool,
}

impl std::str::FromStr for AspectRatio {
impl core::str::FromStr for AspectRatio {
type Err = Error;

fn from_str(text: &str) -> Result<Self, Error> {
Expand Down Expand Up @@ -109,7 +109,7 @@ impl Default for AspectRatio {
#[cfg(test)]
mod tests {
use super::*;
use std::str::FromStr;
use core::str::FromStr;

macro_rules! test {
($name:ident, $text:expr, $result:expr) => (
Expand Down
9 changes: 6 additions & 3 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

use crate::{colors, ByteExt, Error, Stream};

#[cfg(not(feature = "std"))]
use kurbo::common::FloatFuncs;

/// Representation of the [`<color>`] type.
///
/// [`<color>`]: https://www.w3.org/TR/css-color-3/
Expand Down Expand Up @@ -75,7 +78,7 @@ impl Color {
}
}

impl std::str::FromStr for Color {
impl core::str::FromStr for Color {
type Err = Error;

/// Parses [CSS3](https://www.w3.org/TR/css-color-3/) `Color` from a string.
Expand Down Expand Up @@ -300,7 +303,7 @@ fn hue_to_rgb(t1: f32, t2: f32, mut hue: f32) -> f32 {

#[inline]
fn bound<T: Ord>(min: T, val: T, max: T) -> T {
std::cmp::max(min, std::cmp::min(max, val))
core::cmp::max(min, core::cmp::min(max, val))
}

#[inline]
Expand All @@ -314,7 +317,7 @@ fn f64_bound(min: f64, val: f64, max: f64) -> f64 {
#[rustfmt::skip]
#[cfg(test)]
mod tests {
use std::str::FromStr;
use core::str::FromStr;
use crate::Color;

macro_rules! test {
Expand Down
2 changes: 1 addition & 1 deletion src/colors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ pub fn from_str(text: &str) -> Option<Color> {
//
// https://github.com/sfackler/rust-phf

use std::hash::Hasher;
use core::hash::Hasher;

pub struct Map<V: 'static> {
pub key: u64,
Expand Down
6 changes: 4 additions & 2 deletions src/directional_position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// SPDX-License-Identifier: Apache-2.0 OR MIT

use crate::{Error, Length, LengthUnit, Stream};
use alloc::string::ToString;
use alloc::vec;

/// List of all SVG directional positions.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
Expand Down Expand Up @@ -52,7 +54,7 @@ impl From<DirectionalPosition> for Length {
}
}

impl std::str::FromStr for DirectionalPosition {
impl core::str::FromStr for DirectionalPosition {
type Err = Error;

#[inline]
Expand Down Expand Up @@ -108,7 +110,7 @@ impl Stream<'_> {
#[cfg(test)]
mod tests {
use super::*;
use std::str::FromStr;
use core::str::FromStr;

macro_rules! test_p {
($name:ident, $text:expr, $result:expr) => (
Expand Down
4 changes: 2 additions & 2 deletions src/enable_background.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub enum EnableBackground {
},
}

impl std::str::FromStr for EnableBackground {
impl core::str::FromStr for EnableBackground {
type Err = Error;

fn from_str(text: &str) -> Result<Self, Self::Err> {
Expand Down Expand Up @@ -71,7 +71,7 @@ impl std::str::FromStr for EnableBackground {
#[cfg(test)]
mod tests {
use super::*;
use std::str::FromStr;
use core::str::FromStr;

#[test]
fn parse_1() {
Expand Down
10 changes: 7 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// Copyright 2018 the SVG Types Authors
// SPDX-License-Identifier: Apache-2.0 OR MIT

use alloc::string::String;
use alloc::vec;
use alloc::vec::Vec;

/// List of all errors.
#[derive(Debug, PartialEq, Eq)]
pub enum Error {
Expand Down Expand Up @@ -46,8 +50,8 @@ pub enum Error {
InvalidNumber(usize),
}

impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl core::fmt::Display for Error {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match *self {
Error::UnexpectedEndOfStream => {
write!(f, "unexpected end of stream")
Expand Down Expand Up @@ -93,7 +97,7 @@ impl std::fmt::Display for Error {
}
}

impl std::error::Error for Error {
impl core::error::Error for Error {
fn description(&self) -> &str {
"an SVG data parsing error"
}
Expand Down
6 changes: 3 additions & 3 deletions src/filter_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ impl From<Error> for FilterValueListParserError {
}
}

impl std::fmt::Display for FilterValueListParserError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl core::fmt::Display for FilterValueListParserError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match *self {
FilterValueListParserError::PercentageValue(pos) => {
write!(f, "a percentage value detected at position {}", pos)
Expand Down Expand Up @@ -102,7 +102,7 @@ impl std::fmt::Display for FilterValueListParserError {
}
}

impl std::error::Error for FilterValueListParserError {
impl core::error::Error for FilterValueListParserError {
fn description(&self) -> &str {
"filter-value-list parsing error"
}
Expand Down
8 changes: 6 additions & 2 deletions src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@

use crate::stream::{ByteExt, Stream};
use crate::Error;
use std::fmt::Display;
use alloc::format;
use alloc::string::{String, ToString};
use alloc::vec;
use alloc::vec::Vec;
use core::fmt::Display;

/// Parses a list of font families and generic families from a string.
pub fn parse_font_families(text: &str) -> Result<Vec<FontFamily>, Error> {
Expand Down Expand Up @@ -36,7 +40,7 @@ pub enum FontFamily {
}

impl Display for FontFamily {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let str = match self {
FontFamily::Monospace => "monospace".to_string(),
FontFamily::Serif => "serif".to_string(),
Expand Down
4 changes: 2 additions & 2 deletions src/length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl Default for Length {
}
}

impl std::str::FromStr for Length {
impl core::str::FromStr for Length {
type Err = Error;

#[inline]
Expand Down Expand Up @@ -187,7 +187,7 @@ impl Iterator for LengthListParser<'_> {
#[cfg(test)]
mod tests {
use super::*;
use std::str::FromStr;
use core::str::FromStr;

macro_rules! test_p {
($name:ident, $text:expr, $result:expr) => (
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,14 @@
None.
*/

#![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
#![forbid(unsafe_code)]
#![deny(missing_docs)]
#![deny(missing_debug_implementations)]
#![deny(missing_copy_implementations)]

extern crate alloc;

macro_rules! matches {
($expression:expr, $($pattern:tt)+) => {
match $expression {
Expand Down
4 changes: 2 additions & 2 deletions src/number.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// Copyright 2018 the SVG Types Authors
// SPDX-License-Identifier: Apache-2.0 OR MIT

use std::str::FromStr;
use core::str::FromStr;

use crate::{ByteExt, Error, Stream};

/// An [SVG number](https://www.w3.org/TR/SVG2/types.html#InterfaceSVGNumber).
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct Number(pub f64);

impl std::str::FromStr for Number {
impl core::str::FromStr for Number {
type Err = Error;

fn from_str(text: &str) -> Result<Self, Self::Err> {
Expand Down
2 changes: 1 addition & 1 deletion src/paint.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2018 the SVG Types Authors
// SPDX-License-Identifier: Apache-2.0 OR MIT

use std::str::FromStr;
use core::str::FromStr;

use crate::{Color, Error, Stream};

Expand Down
6 changes: 4 additions & 2 deletions src/paint_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// SPDX-License-Identifier: Apache-2.0 OR MIT

use crate::stream::Stream;
use alloc::vec;
use alloc::vec::Vec;

/// [`paint-order`] property variants.
///
Expand Down Expand Up @@ -45,7 +47,7 @@ impl From<[PaintOrderKind; 3]> for PaintOrder {
}
}

impl std::str::FromStr for PaintOrder {
impl core::str::FromStr for PaintOrder {
type Err = ();

/// Parses `PaintOrder` from a string.
Expand Down Expand Up @@ -112,7 +114,7 @@ impl std::str::FromStr for PaintOrder {
#[cfg(test)]
mod tests {
use super::*;
use std::str::FromStr;
use core::str::FromStr;

#[test]
fn parse_1() {
Expand Down
Loading

0 comments on commit 5efb018

Please sign in to comment.