Skip to content

Commit

Permalink
provide no_std support
Browse files Browse the repository at this point in the history
  • Loading branch information
mspiegel committed Jul 17, 2024
1 parent e9373de commit 9db1c56
Show file tree
Hide file tree
Showing 15 changed files with 72 additions and 17 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,21 @@ jobs:
with:
command: test

no_std:
name: Test Suite (no_std)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- uses: actions-rs/cargo@v1
with:
command: test
args: --no-default-features

fmt:
name: Rustfmt
runs-on: ubuntu-latest
Expand Down
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ image = { version = "0.25", default-features = false, optional = true }
image = "0.25"

[features]
default = ["image", "svg", "pic"]
default = ["std", "image", "svg", "pic"]
image = ["dep:image", "std"]
std = []
bench = []
svg = []
pic = []
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ The default settings will depend on the `image` crate. If you don't need image g

```toml
[dependencies]
qrcode = { version = "0.14.1", default-features = false }
qrcode = { version = "0.14.1", default-features = false, features = ["std"] }
```

Example
Expand Down
12 changes: 11 additions & 1 deletion src/bits.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! The `bits` module encodes binary data into raw bits used in a QR code.
use std::cmp::min;
use alloc::vec::Vec;
use core::cmp::min;

#[cfg(feature = "bench")]
extern crate test;
Expand Down Expand Up @@ -117,6 +118,7 @@ impl Bits {

#[test]
fn test_push_number() {
use alloc::vec;
let mut bits = Bits::new(Version::Normal(1));

bits.push_number(3, 0b010); // 0:0 .. 0:3
Expand Down Expand Up @@ -281,6 +283,7 @@ impl Bits {
mod eci_tests {
use crate::bits::Bits;
use crate::types::{QrError, Version};
use alloc::vec;

#[test]
fn test_9() {
Expand Down Expand Up @@ -351,6 +354,7 @@ impl Bits {
mod numeric_tests {
use crate::bits::Bits;
use crate::types::{QrError, Version};
use alloc::vec;

#[test]
fn test_iso_18004_2006_example_1() {
Expand Down Expand Up @@ -459,6 +463,7 @@ impl Bits {
mod alphanumeric_tests {
use crate::bits::Bits;
use crate::types::{QrError, Version};
use alloc::vec;

#[test]
fn test_iso_18004_2006_example() {
Expand Down Expand Up @@ -506,6 +511,7 @@ impl Bits {
mod byte_tests {
use crate::bits::Bits;
use crate::types::{QrError, Version};
use alloc::vec;

#[test]
fn test() {
Expand Down Expand Up @@ -573,6 +579,7 @@ impl Bits {
mod kanji_tests {
use crate::bits::Bits;
use crate::types::{QrError, Version};
use alloc::vec;

#[test]
fn test_iso_18004_example() {
Expand Down Expand Up @@ -758,6 +765,7 @@ impl Bits {
mod finish_tests {
use crate::bits::Bits;
use crate::types::{EcLevel, QrError, Version};
use alloc::vec;

#[test]
fn test_hello_world() {
Expand Down Expand Up @@ -857,6 +865,8 @@ impl Bits {
mod encode_tests {
use crate::bits::Bits;
use crate::types::{EcLevel, QrError, QrResult, Version};
use alloc::vec;
use alloc::vec::Vec;

fn encode(data: &[u8], version: Version, ec_level: EcLevel) -> QrResult<Vec<u8>> {
let mut bits = Bits::new(version);
Expand Down
11 changes: 8 additions & 3 deletions src/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
//! let bools = c.to_bools();
//! ```
use std::{cmp::max, iter};
use alloc::boxed::Box;
use alloc::vec;
use alloc::vec::Vec;
use core::{cmp::max, iter};

use crate::cast::As;
use crate::types::{Color, EcLevel, Version};
Expand Down Expand Up @@ -102,9 +105,9 @@ impl Canvas {

/// Converts the canvas into a human-readable string.
#[cfg(test)]
fn to_debug_str(&self) -> String {
fn to_debug_str(&self) -> alloc::string::String {
let width = self.width;
let mut res = String::with_capacity((width * (width + 1)) as usize);
let mut res = alloc::string::String::with_capacity((width * (width + 1)) as usize);
for y in 0..width {
res.push('\n');
for x in 0..width {
Expand Down Expand Up @@ -1190,6 +1193,8 @@ impl Iterator for DataModuleIter {
mod data_iter_tests {
use crate::canvas::DataModuleIter;
use crate::types::Version;
use alloc::vec::Vec;
use alloc::vec;

#[test]
fn test_qr() {
Expand Down
3 changes: 2 additions & 1 deletion src/ec.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! The `ec` module applies the Reed-Solomon error correction codes.
use std::ops::Deref;
use alloc::vec::Vec;
use core::ops::Deref;

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

Expand Down
7 changes: 6 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
//! # }
//! ```
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(feature = "bench", feature(test))] // Unstable libraries
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![warn(clippy::pedantic)]
Expand All @@ -34,7 +35,11 @@
#![cfg_attr(feature = "bench", doc = include_str!("../README.md"))]
// ^ make sure we can test our README.md.

use std::ops::Index;
extern crate alloc;

use alloc::string::String;
use alloc::vec::Vec;
use core::ops::Index;

pub mod bits;
pub mod canvas;
Expand Down
4 changes: 3 additions & 1 deletion src/optimize.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Find the optimal data mode sequence to encode a piece of data.
use crate::types::{Mode, Version};
use std::slice::Iter;
use core::slice::Iter;

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

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

fn test_optimization_result(given: &[Segment], expected: &[Segment], version: Version) {
let prev_len = total_encoded_len(given, version);
Expand Down
2 changes: 2 additions & 0 deletions src/render/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use crate::types::Color;

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

use alloc::vec::Vec;

// need to keep using this macro to implement Pixel separately for each color model,
// otherwise we'll have conflicting impl with `impl Pixel for impl Element` 🤷
macro_rules! impl_pixel_for_image_pixel {
Expand Down
2 changes: 1 addition & 1 deletion src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use crate::cast::As;
use crate::types::Color;
use std::cmp::max;
use core::cmp::max;

pub mod image;
pub mod pic;
Expand Down
4 changes: 3 additions & 1 deletion src/render/pic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
#![cfg(feature = "pic")]

use std::fmt::Write;
use alloc::format;
use alloc::string::String;
use core::fmt::Write;

use crate::render::{Canvas as RenderCanvas, Pixel};
use crate::types::Color as ModuleColor;
Expand Down
4 changes: 4 additions & 0 deletions src/render/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ use crate::cast::As;
use crate::render::{Canvas as RenderCanvas, Pixel};
use crate::types::Color;

use alloc::string::String;
use alloc::vec;
use alloc::vec::Vec;

pub trait Element: Copy {
fn default_color(color: Color) -> Self;
fn strlen(self) -> usize;
Expand Down
6 changes: 4 additions & 2 deletions src/render/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
#![cfg(feature = "svg")]

use std::fmt::Write;
use std::marker::PhantomData;
use alloc::format;
use alloc::string::String;
use core::fmt::Write;
use core::marker::PhantomData;

use crate::render::{Canvas as RenderCanvas, Pixel};
use crate::types::Color as ModuleColor;
Expand Down
4 changes: 4 additions & 0 deletions src/render/unicode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
use crate::render::{Canvas as RenderCanvas, Color, Pixel};

use alloc::string::String;
use alloc::vec;
use alloc::vec::Vec;

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

#[derive(Copy, Clone, PartialEq, Eq)]
Expand Down
9 changes: 5 additions & 4 deletions src/types.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::cast::As;
use std::cmp::{Ordering, PartialOrd};
use std::default::Default;
use std::fmt::{Display, Error, Formatter};
use std::ops::Not;
use core::cmp::{Ordering, PartialOrd};
use core::default::Default;
use core::fmt::{Display, Error, Formatter};
use core::ops::Not;

//------------------------------------------------------------------------------
//{{{ QrResult
Expand Down Expand Up @@ -41,6 +41,7 @@ impl Display for QrError {
}
}

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

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

0 comments on commit 9db1c56

Please sign in to comment.