Skip to content

Commit 0688cd2

Browse files
Support no_std
1 parent 62d0847 commit 0688cd2

22 files changed

+98
-50
lines changed

.github/workflows/ci.yml

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ env:
1212
# List of packages that will be checked with the minimum supported Rust version.
1313
# This should be limited to packages that are intended for publishing.
1414
RUST_MIN_VER_PKGS: "-p svgtypes"
15+
# List of features that depend on the standard library and will be excluded from no_std checks.
16+
FEATURES_DEPENDING_ON_STD: "std,default"
1517

1618

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

114+
- name: cargo clippy (no_std)
115+
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
116+
108117
- name: cargo clippy
109-
run: cargo hack clippy --workspace --locked --optional-deps --each-feature -- -D warnings
118+
run: cargo hack clippy --workspace --locked --optional-deps --each-feature --ignore-unknown-features --features std -- -D warnings
110119

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

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

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

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

143152
test-stable:
144153
name: cargo test
@@ -216,8 +225,11 @@ jobs:
216225
with:
217226
save-if: ${{ github.event_name != 'merge_group' }}
218227

228+
- name: cargo check (no_std)
229+
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
230+
219231
- name: cargo check
220-
run: cargo hack check ${{ env.RUST_MIN_VER_PKGS }} --locked --optional-deps --each-feature
232+
run: cargo hack check ${{ env.RUST_MIN_VER_PKGS }} --locked --optional-deps --each-feature --ignore-unknown-features --features std
221233

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

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

247259
doc:
248260
name: cargo doc

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ exclude = ["benches/", "codegen/", "fuzz/"]
1515
[workspace]
1616
members = ["benches"]
1717

18+
[features]
19+
default = ["std"]
20+
std = ["kurbo/std"]
21+
libm = ["kurbo/libm"]
22+
1823
[dependencies]
1924
siphasher = "1.0" # perfect hash implementation for color names
20-
kurbo = "0.11" # ArcTo to CurveTo(s)
25+
kurbo = { version = "0.11", default-features = false } # ArcTo to CurveTo(s)

src/angle.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl Angle {
4242
}
4343
}
4444

45-
impl std::str::FromStr for Angle {
45+
impl core::str::FromStr for Angle {
4646
type Err = Error;
4747

4848
#[inline]
@@ -99,7 +99,7 @@ impl Stream<'_> {
9999
#[cfg(test)]
100100
mod tests {
101101
use super::*;
102-
use std::str::FromStr;
102+
use core::str::FromStr;
103103

104104
macro_rules! test_p {
105105
($name:ident, $text:expr, $result:expr) => (

src/aspect_ratio.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub struct AspectRatio {
4141
pub slice: bool,
4242
}
4343

44-
impl std::str::FromStr for AspectRatio {
44+
impl core::str::FromStr for AspectRatio {
4545
type Err = Error;
4646

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

114114
macro_rules! test {
115115
($name:ident, $text:expr, $result:expr) => (

src/color.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

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

6+
#[cfg(not(feature = "std"))]
7+
use kurbo::common::FloatFuncs;
8+
69
/// Representation of the [`<color>`] type.
710
///
811
/// [`<color>`]: https://www.w3.org/TR/css-color-3/
@@ -75,7 +78,7 @@ impl Color {
7578
}
7679
}
7780

78-
impl std::str::FromStr for Color {
81+
impl core::str::FromStr for Color {
7982
type Err = Error;
8083

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

301304
#[inline]
302305
fn bound<T: Ord>(min: T, val: T, max: T) -> T {
303-
std::cmp::max(min, std::cmp::min(max, val))
306+
core::cmp::max(min, core::cmp::min(max, val))
304307
}
305308

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

320323
macro_rules! test {

src/colors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ pub fn from_str(text: &str) -> Option<Color> {
199199
//
200200
// https://github.com/sfackler/rust-phf
201201

202-
use std::hash::Hasher;
202+
use core::hash::Hasher;
203203

204204
pub struct Map<V: 'static> {
205205
pub key: u64,

src/directional_position.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// SPDX-License-Identifier: Apache-2.0 OR MIT
33

44
use crate::{Error, Length, LengthUnit, Stream};
5+
use alloc::string::ToString;
6+
use alloc::vec;
57

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

55-
impl std::str::FromStr for DirectionalPosition {
57+
impl core::str::FromStr for DirectionalPosition {
5658
type Err = Error;
5759

5860
#[inline]
@@ -108,7 +110,7 @@ impl Stream<'_> {
108110
#[cfg(test)]
109111
mod tests {
110112
use super::*;
111-
use std::str::FromStr;
113+
use core::str::FromStr;
112114

113115
macro_rules! test_p {
114116
($name:ident, $text:expr, $result:expr) => (

src/enable_background.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub enum EnableBackground {
1919
},
2020
}
2121

22-
impl std::str::FromStr for EnableBackground {
22+
impl core::str::FromStr for EnableBackground {
2323
type Err = Error;
2424

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

7676
#[test]
7777
fn parse_1() {

src/error.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// Copyright 2018 the SVG Types Authors
22
// SPDX-License-Identifier: Apache-2.0 OR MIT
33

4+
use alloc::string::String;
5+
use alloc::vec;
6+
use alloc::vec::Vec;
7+
48
/// List of all errors.
59
#[derive(Debug, PartialEq, Eq)]
610
pub enum Error {
@@ -46,8 +50,8 @@ pub enum Error {
4650
InvalidNumber(usize),
4751
}
4852

49-
impl std::fmt::Display for Error {
50-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
53+
impl core::fmt::Display for Error {
54+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
5155
match *self {
5256
Error::UnexpectedEndOfStream => {
5357
write!(f, "unexpected end of stream")
@@ -93,7 +97,7 @@ impl std::fmt::Display for Error {
9397
}
9498
}
9599

96-
impl std::error::Error for Error {
100+
impl core::error::Error for Error {
97101
fn description(&self) -> &str {
98102
"an SVG data parsing error"
99103
}

0 commit comments

Comments
 (0)