Skip to content

Commit ba55d47

Browse files
committed
make std optional
1 parent 660aa1a commit ba55d47

File tree

4 files changed

+32
-14
lines changed

4 files changed

+32
-14
lines changed

Cargo.toml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,16 @@ lto = "thin"
2121
codegen-units = 1
2222

2323
[features]
24-
default = []
25-
srgb_lut = [] # bool
26-
color_double_precision = []
24+
default = ["std"]
25+
# Enable the Rust standard library. On by default for convenience.
26+
std = ["alloc"]
27+
# Things that need a heap but not full std
28+
alloc = []
29+
# Optional: serde support (works in no_std)
2730
serde = ["dep:serde"]
31+
32+
# Whether to use a lookup table for srgb conversions.
33+
# Useful if the CPU is being used for lots of calculations rather than the GPU.
34+
srgb_lut = []
35+
# Whether to use f64s for all color conversion functions rather than f32.
36+
color_double_precision = []

src/color/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![cfg_attr(not(feature = "std"), no_std)]
2+
13
#[cfg(feature = "srgb_lut")]
24
pub mod lut;
35
pub mod model;

src/color/model.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
use std::fmt::{self};
1010

11+
#[cfg(feature = "alloc")]
12+
extern crate alloc;
13+
1114
use crate::color::ColorFloat;
1215

1316
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -290,13 +293,15 @@ impl Color {
290293

291294
#[must_use]
292295
#[inline]
293-
pub fn into_hex6(self) -> String {
296+
#[cfg(feature = "alloc")]
297+
pub fn into_hex6(self) -> alloc::string::String {
294298
format!("{:02x}{:02x}{:02x}", self.r, self.g, self.b)
295299
}
296300

297301
#[must_use]
298302
#[inline]
299-
pub fn into_hex8(self) -> String {
303+
#[cfg(feature = "alloc")]
304+
pub fn into_hex8(self) -> alloc::string::String {
300305
format!("{:02x}{:02x}{:02x}{:02x}", self.r, self.g, self.b, self.a)
301306
}
302307

src/color/parse.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![allow(dead_code)]
22

3-
use std::fmt;
3+
use core::fmt;
44

55
use crate::color::model::Color;
66

@@ -16,15 +16,17 @@ pub enum ColorParseError {
1616
impl fmt::Display for ColorParseError {
1717
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1818
use ColorParseError::*;
19-
match self {
20-
Empty => write!(f, "empty color string"),
21-
InvalidLength => write!(f, "invalid hex length"),
22-
InvalidHex => write!(f, "invalid hex digits"),
23-
InvalidFunc => write!(f, "invalid rgb()/rgba() function"),
24-
OutOfRange => write!(f, "component out of range"),
25-
}
19+
let msg = match self {
20+
Empty => "empty color string",
21+
InvalidLength => "invalid hex length",
22+
InvalidHex => "invalid hex digits",
23+
InvalidFunc => "invalid rgb()/rgba() function",
24+
OutOfRange => "component out of range",
25+
};
26+
f.write_str(msg)
2627
}
2728
}
29+
#[cfg(feature = "std")]
2830
impl std::error::Error for ColorParseError {}
2931

3032
/// Parse a hex color from a string.
@@ -219,7 +221,7 @@ pub fn parse_color(mut s: &str) -> Result<Color, ColorParseError> {
219221
Err(InvalidFunc)
220222
}
221223

222-
impl std::str::FromStr for Color {
224+
impl core::str::FromStr for Color {
223225
type Err = ColorParseError;
224226
fn from_str(s: &str) -> Result<Self, Self::Err> {
225227
parse_color(s)

0 commit comments

Comments
 (0)