Skip to content

Commit f9ca7b8

Browse files
committed
Make create no_std compatible
1 parent b75ce6a commit f9ca7b8

12 files changed

+34
-23
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ inherits = "release"
3232
debug = true
3333

3434
[features]
35+
default = ["std"]
3536
bench = []
3637
dummy_match_byte = []
3738
# Useful for skipping tests when execution is slow, e.g., under miri
3839
skip_long_tests = []
40+
std = []
3941

4042
[workspace]
4143
members = [".", "./macros", "./color"]

color/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* This Source Code Form is subject to the terms of the Mozilla Public
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4-
4+
#![cfg_attr(not(test), no_std)]
55
#![deny(missing_docs)]
66

77
//! Fairly complete css-color implementation.
@@ -11,13 +11,13 @@
1111
#[cfg(test)]
1212
mod tests;
1313

14+
use core::f32::consts::PI;
15+
use core::fmt;
1416
use cssparser::color::{
1517
clamp_floor_256_f32, clamp_unit_f32, parse_hash_color, serialize_color_alpha,
1618
PredefinedColorSpace, OPAQUE,
1719
};
1820
use cssparser::{match_ignore_ascii_case, CowRcStr, ParseError, Parser, ToCss, Token};
19-
use std::f32::consts::PI;
20-
use std::fmt;
2121

2222
/// Return the named color with the given name.
2323
///

src/color.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
pub const OPAQUE: f32 = 1.0;
1616

1717
use crate::{BasicParseError, Parser, ToCss, Token};
18-
use std::fmt;
18+
use core::fmt;
1919

2020
/// Clamp a 0..1 number to a 0..255 range to u8.
2121
///

src/cow_rc_str.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5-
use std::borrow::{Borrow, Cow};
6-
use std::rc::Rc;
7-
use std::{cmp, fmt, hash, marker, mem, ops, ptr, slice, str};
5+
use alloc::borrow::{Borrow, Cow};
6+
use alloc::rc::Rc;
7+
use alloc::string::String;
8+
use core::{cmp, fmt, hash, marker, mem, ops, ptr, slice, str};
89

910
/// A string that is either shared (heap-allocated and reference-counted) or borrowed.
1011
///

src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* This Source Code Form is subject to the terms of the Mozilla Public
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4-
4+
#![cfg_attr(not(feature = "std"), no_std)]
55
#![crate_name = "cssparser"]
66
#![crate_type = "rlib"]
77
#![cfg_attr(feature = "bench", feature(test))]
@@ -67,6 +67,8 @@ fn parse_border_spacing(_context: &ParserContext, input: &mut Parser)
6767

6868
#![recursion_limit = "200"] // For color::parse_color_keyword
6969

70+
extern crate alloc;
71+
7072
pub use crate::cow_rc_str::CowRcStr;
7173
pub use crate::from_bytes::{stylesheet_encoding, EncodingSupport};
7274
#[doc(hidden)]

src/macros.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5-
use std::mem::MaybeUninit;
5+
use core::mem::MaybeUninit;
66

77
/// Expands to a `match` expression with string patterns,
88
/// matching case-insensitively in the ASCII range.
@@ -191,7 +191,7 @@ pub fn _cssparser_internal_to_lowercase<'a>(
191191
// `buffer` was initialized to a copy of `input`
192192
// (which is `&str` so well-formed UTF-8)
193193
// then ASCII-lowercased (which preserves UTF-8 well-formedness):
194-
unsafe { ::std::str::from_utf8_unchecked(buffer) }
194+
unsafe { ::core::str::from_utf8_unchecked(buffer) }
195195
}
196196

197197
Some(

src/parser.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55
use crate::cow_rc_str::CowRcStr;
66
use crate::tokenizer::{SourceLocation, SourcePosition, Token, Tokenizer};
7+
use alloc::vec::Vec;
8+
use core::fmt;
9+
use core::ops::BitOr;
10+
use core::ops::Range;
711
use smallvec::SmallVec;
8-
use std::fmt;
9-
use std::ops::BitOr;
10-
use std::ops::Range;
1112

1213
/// A capture of the internal state of a `Parser` (including the position within the input),
1314
/// obtained from the `Parser::position` method.
@@ -224,6 +225,7 @@ impl<'i, E: fmt::Display> fmt::Display for ParseError<'i, E> {
224225
}
225226
}
226227

228+
#[cfg(feature = "std")]
227229
impl<'i, E: fmt::Display + fmt::Debug> std::error::Error for ParseError<'i, E> {}
228230

229231
/// The owned input for a parser.

src/rules_and_declarations.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ pub struct RuleBodyParser<'i, 't, 'a, P, I, E> {
199199
/// The parser given to `DeclarationListParser::new`
200200
pub parser: &'a mut P,
201201

202-
_phantom: std::marker::PhantomData<(I, E)>,
202+
_phantom: core::marker::PhantomData<(I, E)>,
203203
}
204204

205205
/// A parser for a rule body item.
@@ -235,7 +235,7 @@ impl<'i, 't, 'a, P, I, E> RuleBodyParser<'i, 't, 'a, P, I, E> {
235235
Self {
236236
input,
237237
parser,
238-
_phantom: std::marker::PhantomData,
238+
_phantom: core::marker::PhantomData,
239239
}
240240
}
241241
}

src/serializer.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

55
use crate::match_byte;
6+
use alloc::string::String;
7+
use core::fmt::{self, Write};
8+
use core::str;
69
use dtoa_short::Notation;
7-
use std::fmt::{self, Write};
8-
use std::str;
910

1011
use super::Token;
1112

src/size_of_tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ macro_rules! size_of_test {
99
($testname: ident, $t: ty, $expected_min_size: expr, $expected_max_size: expr) => {
1010
#[test]
1111
fn $testname() {
12-
let new = ::std::mem::size_of::<$t>();
12+
let new = ::core::mem::size_of::<$t>();
1313
if new < $expected_min_size {
1414
panic!(
1515
"Your changes have decreased the stack size of {} from {} to {}. \
@@ -39,7 +39,7 @@ macro_rules! size_of_test {
3939

4040
// Some of these assume 64-bit
4141
size_of_test!(token, Token, 32);
42-
size_of_test!(std_cow_str, std::borrow::Cow<'static, str>, 24, 32);
42+
size_of_test!(std_cow_str, alloc::borrow::Cow<'static, str>, 24, 32);
4343
size_of_test!(cow_rc_str, CowRcStr, 16);
4444

4545
size_of_test!(tokenizer, crate::tokenizer::Tokenizer, 72);

src/tokenizer.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
use self::Token::*;
88
use crate::cow_rc_str::CowRcStr;
99
use crate::parser::ParserState;
10-
use std::char;
11-
use std::ops::Range;
10+
use alloc::borrow::ToOwned;
11+
use alloc::string::String;
12+
use alloc::vec::Vec;
13+
use core::char;
14+
use core::ops::Range;
1215

1316
#[cfg(not(feature = "dummy_match_byte"))]
1417
use cssparser_macros::match_byte;

src/unicode_range.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
77
use crate::tokenizer::Token;
88
use crate::{BasicParseError, Parser, ToCss};
9-
use std::char;
10-
use std::fmt;
9+
use core::char;
10+
use core::fmt;
1111

1212
/// One contiguous range of code points.
1313
///

0 commit comments

Comments
 (0)