Skip to content

Commit 1aca86b

Browse files
committed
Add "std" feature flag to allow disabling for no_std environments
Signed-off-by: Ben Krieger <ben.krieger@intel.com>
1 parent faa4397 commit 1aca86b

File tree

11 files changed

+50
-23
lines changed

11 files changed

+50
-23
lines changed

Cargo.toml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ license = "MIT OR Apache-2.0"
55
version = "0.12.0"
66
edition = "2018"
77
authors = ["kennytm <kennytm@gmail.com>"]
8-
keywords = ["qrcode"]
8+
keywords = ["qrcode", "no_std"]
99
repository = "https://github.com/kennytm/qrcode-rust"
1010
readme = "README.md"
1111
documentation = "http://docs.rs/qrcode"
@@ -22,15 +22,17 @@ maintenance = { status = "passively-maintained" }
2222

2323
[dependencies]
2424
image = { version = "0.23", default-features = false, optional = true }
25-
checked_int_cast = "1"
25+
checked_int_cast = { version = "1", optional = true }
2626

2727
[dev-dependencies]
2828
image = "0.23"
2929

3030
[features]
31-
default = ["image", "svg"]
31+
default = ["image", "svg", "std"]
3232
bench = []
33-
svg = []
33+
std = ["dep:checked_int_cast"]
34+
image = ["std", "dep:image"]
35+
svg = ["std"]
3436

3537
[[bin]]
3638
name = "qrencode"

src/bits.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! The `bits` module encodes binary data into raw bits used in a QR code.
22
3-
use std::cmp::min;
3+
use alloc::vec::Vec;
4+
use core::cmp::min;
45

56
#[cfg(feature = "bench")]
67
extern crate test;
@@ -855,6 +856,7 @@ impl Bits {
855856
mod encode_tests {
856857
use crate::bits::Bits;
857858
use crate::types::{EcLevel, QrError, QrResult, Version};
859+
use alloc::vec::Vec;
858860

859861
fn encode(data: &[u8], version: Version, ec_level: EcLevel) -> QrResult<Vec<u8>> {
860862
let mut bits = Bits::new(version);

src/canvas.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@
99
//! c.apply_mask(MaskPattern::Checkerboard);
1010
//! let bools = c.to_bools();
1111
12-
use std::cmp::max;
12+
use alloc::boxed::Box;
13+
use alloc::vec::Vec;
14+
use core::cmp::max;
15+
16+
#[cfg(test)]
17+
use alloc::string::String;
1318

1419
use crate::cast::As;
1520
use crate::types::{Color, EcLevel, Version};
@@ -1188,6 +1193,7 @@ impl Iterator for DataModuleIter {
11881193
mod data_iter_tests {
11891194
use crate::canvas::DataModuleIter;
11901195
use crate::types::Version;
1196+
use alloc::vec::Vec;
11911197

11921198
#[test]
11931199
fn test_qr() {

src/cast.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use std::fmt::Display;
1+
use core::fmt::Display;
22

3-
#[cfg(debug_assertions)]
3+
#[cfg(all(debug_assertions, feature = "std"))]
44
use checked_int_cast::CheckedIntCast;
55

66
pub trait Truncate {
@@ -39,7 +39,7 @@ impl<T> ExpectOrOverflow for Option<T> {
3939

4040
macro_rules! impl_as {
4141
($ty:ty) => {
42-
#[cfg(debug_assertions)]
42+
#[cfg(all(debug_assertions, feature = "std"))]
4343
impl As for $ty {
4444
fn as_u16(self) -> u16 {
4545
self.as_u16_checked().expect_or_overflow(self, "u16")
@@ -62,7 +62,7 @@ macro_rules! impl_as {
6262
}
6363
}
6464

65-
#[cfg(not(debug_assertions))]
65+
#[cfg(any(not(debug_assertions), not(feature = "std")))]
6666
impl As for $ty {
6767
fn as_u16(self) -> u16 {
6868
self as u16

src/ec.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! The `ec` module applies the Reed-Solomon error correction codes.
22
3-
use std::ops::Deref;
3+
use alloc::vec::Vec;
4+
use core::ops::Deref;
45

56
use crate::types::{EcLevel, QrResult, Version};
67

src/lib.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,24 @@
2626
//! println!("{}", string);
2727
//! ```
2828
29-
#![cfg_attr(feature = "bench", feature(test, external_doc))] // Unstable libraries
29+
#![cfg_attr(feature = "bench", feature(test))] // Unstable libraries
3030
#![deny(warnings, clippy::pedantic)]
3131
#![allow(
3232
clippy::must_use_candidate, // This is just annoying.
3333
clippy::use_self, // Rust 1.33 doesn't support Self::EnumVariant, let's try again in 1.37.
3434
)]
35-
#![cfg_attr(feature = "bench", doc(include = "../README.md"))]
35+
#![cfg_attr(all(feature = "bench", feature = "svg", feature = "image"), doc = include_str!("../README.md"))]
3636
// ^ make sure we can test our README.md.
3737

38-
use std::ops::Index;
38+
// Compile without the stdlib unless the std feature is enabled
39+
#![feature(error_in_core)]
40+
#![cfg_attr(not(feature = "std"), no_std)]
41+
#[macro_use]
42+
extern crate alloc;
43+
44+
use alloc::string::String;
45+
use alloc::vec::Vec;
46+
use core::ops::Index;
3947

4048
pub mod bits;
4149
pub mod canvas;
@@ -49,6 +57,7 @@ pub use crate::types::{Color, EcLevel, QrResult, Version};
4957

5058
use crate::cast::As;
5159
use crate::render::{Pixel, Renderer};
60+
#[cfg(feature = "std")]
5261
use checked_int_cast::CheckedIntCast;
5362

5463
/// The encoded QR code symbol.
@@ -185,6 +194,7 @@ impl QrCode {
185194

186195
/// Checks whether a module at coordinate (x, y) is a functional module or
187196
/// not.
197+
#[cfg(feature = "std")]
188198
pub fn is_functional(&self, x: usize, y: usize) -> bool {
189199
let x = x.as_i16_checked().expect("coordinate is too large for QR code");
190200
let y = y.as_i16_checked().expect("coordinate is too large for QR code");
@@ -325,7 +335,7 @@ mod image_tests {
325335
fn test_annex_i_qr_as_image() {
326336
let code = QrCode::new(b"01234567").unwrap();
327337
let image = code.render::<Luma<u8>>().build();
328-
let expected = load_from_memory(include_bytes!("test_annex_i_qr_as_image.png")).unwrap().to_luma();
338+
let expected = load_from_memory(include_bytes!("test_annex_i_qr_as_image.png")).unwrap().to_luma8();
329339
assert_eq!(image.dimensions(), expected.dimensions());
330340
assert_eq!(image.into_raw(), expected.into_raw());
331341
}
@@ -339,7 +349,7 @@ mod image_tests {
339349
.dark_color(Rgb([128, 0, 0]))
340350
.light_color(Rgb([255, 255, 128]))
341351
.build();
342-
let expected = load_from_memory(include_bytes!("test_annex_i_micro_qr_as_image.png")).unwrap().to_rgb();
352+
let expected = load_from_memory(include_bytes!("test_annex_i_micro_qr_as_image.png")).unwrap().to_rgb8();
343353
assert_eq!(image.dimensions(), expected.dimensions());
344354
assert_eq!(image.into_raw(), expected.into_raw());
345355
}

src/optimize.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Find the optimal data mode sequence to encode a piece of data.
22
use crate::types::{Mode, Version};
3-
use std::slice::Iter;
3+
use alloc::slice::Iter;
44

55
#[cfg(feature = "bench")]
66
extern crate test;
@@ -155,6 +155,7 @@ impl<'a> Iterator for Parser<'a> {
155155
mod parse_tests {
156156
use crate::optimize::{Parser, Segment};
157157
use crate::types::Mode;
158+
use alloc::vec::Vec;
158159

159160
fn parse(data: &[u8]) -> Vec<Segment> {
160161
Parser::new(data).collect()
@@ -338,6 +339,7 @@ pub fn total_encoded_len(segments: &[Segment], version: Version) -> usize {
338339
mod optimize_tests {
339340
use crate::optimize::{total_encoded_len, Optimizer, Segment};
340341
use crate::types::{Mode, Version};
342+
use alloc::vec::Vec;
341343

342344
fn test_optimization_result(given: Vec<Segment>, expected: Vec<Segment>, version: Version) {
343345
let prev_len = total_encoded_len(&*given, version);

src/render/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::cast::As;
44
use crate::types::Color;
5-
use std::cmp::max;
5+
use core::cmp::max;
66

77
pub mod image;
88
pub mod string;

src/render/string.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
use crate::cast::As;
44
use crate::render::{Canvas as RenderCanvas, Pixel};
55
use crate::types::Color;
6+
use alloc::string::String;
7+
use alloc::vec::Vec;
68

79
pub trait Element: Copy {
810
fn default_color(color: Color) -> Self;

src/render/unicode.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! UTF-8 rendering, with 2 pixels per symbol.
22
33
use crate::render::{Canvas as RenderCanvas, Color, Pixel};
4+
use alloc::string::String;
5+
use alloc::vec::Vec;
46

57
const CODEPAGE: [&str; 4] = [" ", "\u{2584}", "\u{2580}", "\u{2588}"];
68

src/types.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::cast::As;
2-
use std::cmp::{Ordering, PartialOrd};
3-
use std::default::Default;
4-
use std::fmt::{Display, Error, Formatter};
5-
use std::ops::Not;
2+
use core::cmp::{Ordering, PartialOrd};
3+
use core::default::Default;
4+
use core::fmt::{Display, Error, Formatter};
5+
use core::ops::Not;
66

77
//------------------------------------------------------------------------------
88
//{{{ QrResult
@@ -41,7 +41,7 @@ impl Display for QrError {
4141
}
4242
}
4343

44-
impl ::std::error::Error for QrError {}
44+
impl ::core::error::Error for QrError {}
4545

4646
/// `QrResult` is a convenient alias for a QR code generation result.
4747
pub type QrResult<T> = Result<T, QrError>;

0 commit comments

Comments
 (0)