Skip to content

Commit 85b51f5

Browse files
authored
Merge pull request #73 from mspiegel/no_std
provide `no_std` support
2 parents e9373de + 9db1c56 commit 85b51f5

File tree

15 files changed

+72
-17
lines changed

15 files changed

+72
-17
lines changed

.github/workflows/rust.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,21 @@ jobs:
4242
with:
4343
command: test
4444

45+
no_std:
46+
name: Test Suite (no_std)
47+
runs-on: ubuntu-latest
48+
steps:
49+
- uses: actions/checkout@v2
50+
- uses: actions-rs/toolchain@v1
51+
with:
52+
profile: minimal
53+
toolchain: stable
54+
override: true
55+
- uses: actions-rs/cargo@v1
56+
with:
57+
command: test
58+
args: --no-default-features
59+
4560
fmt:
4661
name: Rustfmt
4762
runs-on: ubuntu-latest

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ image = { version = "0.25", default-features = false, optional = true }
2525
image = "0.25"
2626

2727
[features]
28-
default = ["image", "svg", "pic"]
28+
default = ["std", "image", "svg", "pic"]
29+
image = ["dep:image", "std"]
30+
std = []
2931
bench = []
3032
svg = []
3133
pic = []

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ The default settings will depend on the `image` crate. If you don't need image g
1919

2020
```toml
2121
[dependencies]
22-
qrcode = { version = "0.14.1", default-features = false }
22+
qrcode = { version = "0.14.1", default-features = false, features = ["std"] }
2323
```
2424

2525
Example

src/bits.rs

Lines changed: 11 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;
@@ -117,6 +118,7 @@ impl Bits {
117118

118119
#[test]
119120
fn test_push_number() {
121+
use alloc::vec;
120122
let mut bits = Bits::new(Version::Normal(1));
121123

122124
bits.push_number(3, 0b010); // 0:0 .. 0:3
@@ -281,6 +283,7 @@ impl Bits {
281283
mod eci_tests {
282284
use crate::bits::Bits;
283285
use crate::types::{QrError, Version};
286+
use alloc::vec;
284287

285288
#[test]
286289
fn test_9() {
@@ -351,6 +354,7 @@ impl Bits {
351354
mod numeric_tests {
352355
use crate::bits::Bits;
353356
use crate::types::{QrError, Version};
357+
use alloc::vec;
354358

355359
#[test]
356360
fn test_iso_18004_2006_example_1() {
@@ -459,6 +463,7 @@ impl Bits {
459463
mod alphanumeric_tests {
460464
use crate::bits::Bits;
461465
use crate::types::{QrError, Version};
466+
use alloc::vec;
462467

463468
#[test]
464469
fn test_iso_18004_2006_example() {
@@ -506,6 +511,7 @@ impl Bits {
506511
mod byte_tests {
507512
use crate::bits::Bits;
508513
use crate::types::{QrError, Version};
514+
use alloc::vec;
509515

510516
#[test]
511517
fn test() {
@@ -573,6 +579,7 @@ impl Bits {
573579
mod kanji_tests {
574580
use crate::bits::Bits;
575581
use crate::types::{QrError, Version};
582+
use alloc::vec;
576583

577584
#[test]
578585
fn test_iso_18004_example() {
@@ -758,6 +765,7 @@ impl Bits {
758765
mod finish_tests {
759766
use crate::bits::Bits;
760767
use crate::types::{EcLevel, QrError, Version};
768+
use alloc::vec;
761769

762770
#[test]
763771
fn test_hello_world() {
@@ -857,6 +865,8 @@ impl Bits {
857865
mod encode_tests {
858866
use crate::bits::Bits;
859867
use crate::types::{EcLevel, QrError, QrResult, Version};
868+
use alloc::vec;
869+
use alloc::vec::Vec;
860870

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

src/canvas.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
//! let bools = c.to_bools();
1212
//! ```
1313
14-
use std::{cmp::max, iter};
14+
use alloc::boxed::Box;
15+
use alloc::vec;
16+
use alloc::vec::Vec;
17+
use core::{cmp::max, iter};
1518

1619
use crate::cast::As;
1720
use crate::types::{Color, EcLevel, Version};
@@ -102,9 +105,9 @@ impl Canvas {
102105

103106
/// Converts the canvas into a human-readable string.
104107
#[cfg(test)]
105-
fn to_debug_str(&self) -> String {
108+
fn to_debug_str(&self) -> alloc::string::String {
106109
let width = self.width;
107-
let mut res = String::with_capacity((width * (width + 1)) as usize);
110+
let mut res = alloc::string::String::with_capacity((width * (width + 1)) as usize);
108111
for y in 0..width {
109112
res.push('\n');
110113
for x in 0..width {
@@ -1190,6 +1193,8 @@ impl Iterator for DataModuleIter {
11901193
mod data_iter_tests {
11911194
use crate::canvas::DataModuleIter;
11921195
use crate::types::Version;
1196+
use alloc::vec::Vec;
1197+
use alloc::vec;
11931198

11941199
#[test]
11951200
fn test_qr() {

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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
//! # }
2626
//! ```
2727
28+
#![cfg_attr(not(feature = "std"), no_std)]
2829
#![cfg_attr(feature = "bench", feature(test))] // Unstable libraries
2930
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
3031
#![warn(clippy::pedantic)]
@@ -34,7 +35,11 @@
3435
#![cfg_attr(feature = "bench", doc = include_str!("../README.md"))]
3536
// ^ make sure we can test our README.md.
3637

37-
use std::ops::Index;
38+
extern crate alloc;
39+
40+
use alloc::string::String;
41+
use alloc::vec::Vec;
42+
use core::ops::Index;
3843

3944
pub mod bits;
4045
pub mod canvas;

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 core::slice::Iter;
44

55
#[cfg(feature = "bench")]
66
extern crate test;
@@ -160,6 +160,7 @@ impl<'a> Iterator for Parser<'a> {
160160
mod parse_tests {
161161
use crate::optimize::{Parser, Segment};
162162
use crate::types::Mode;
163+
use alloc::vec::Vec;
163164

164165
fn parse(data: &[u8]) -> Vec<Segment> {
165166
Parser::new(data).collect()
@@ -342,6 +343,7 @@ pub fn total_encoded_len(segments: &[Segment], version: Version) -> usize {
342343
mod optimize_tests {
343344
use crate::optimize::{total_encoded_len, Optimizer, Segment};
344345
use crate::types::{Mode, Version};
346+
use alloc::vec::Vec;
345347

346348
fn test_optimization_result(given: &[Segment], expected: &[Segment], version: Version) {
347349
let prev_len = total_encoded_len(given, version);

src/render/image.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use crate::types::Color;
55

66
use image::{ImageBuffer, Luma, LumaA, Primitive, Rgb, Rgba};
77

8+
use alloc::vec::Vec;
9+
810
// need to keep using this macro to implement Pixel separately for each color model,
911
// otherwise we'll have conflicting impl with `impl Pixel for impl Element` 🤷
1012
macro_rules! impl_pixel_for_image_pixel {

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 pic;

src/render/pic.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
1313
#![cfg(feature = "pic")]
1414

15-
use std::fmt::Write;
15+
use alloc::format;
16+
use alloc::string::String;
17+
use core::fmt::Write;
1618

1719
use crate::render::{Canvas as RenderCanvas, Pixel};
1820
use crate::types::Color as ModuleColor;

src/render/string.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ use crate::cast::As;
44
use crate::render::{Canvas as RenderCanvas, Pixel};
55
use crate::types::Color;
66

7+
use alloc::string::String;
8+
use alloc::vec;
9+
use alloc::vec::Vec;
10+
711
pub trait Element: Copy {
812
fn default_color(color: Color) -> Self;
913
fn strlen(self) -> usize;

src/render/svg.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
1313
#![cfg(feature = "svg")]
1414

15-
use std::fmt::Write;
16-
use std::marker::PhantomData;
15+
use alloc::format;
16+
use alloc::string::String;
17+
use core::fmt::Write;
18+
use core::marker::PhantomData;
1719

1820
use crate::render::{Canvas as RenderCanvas, Pixel};
1921
use crate::types::Color as ModuleColor;

src/render/unicode.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
33
use crate::render::{Canvas as RenderCanvas, Color, Pixel};
44

5+
use alloc::string::String;
6+
use alloc::vec;
7+
use alloc::vec::Vec;
8+
59
const CODEPAGE: [&str; 4] = [" ", "\u{2584}", "\u{2580}", "\u{2588}"];
610

711
#[derive(Copy, Clone, PartialEq, Eq)]

src/types.rs

Lines changed: 5 additions & 4 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,6 +41,7 @@ impl Display for QrError {
4141
}
4242
}
4343

44+
#[cfg(feature = "std")]
4445
impl ::std::error::Error for QrError {}
4546

4647
/// `QrResult` is a convenient alias for a QR code generation result.

0 commit comments

Comments
 (0)