Skip to content

Commit 2d158a9

Browse files
authored
Merge pull request #1 from mlafeldt/no_std
Support no_std for embedded environments
2 parents 6431adc + 10509f3 commit 2d158a9

File tree

6 files changed

+48
-10
lines changed

6 files changed

+48
-10
lines changed

.github/workflows/rust.yaml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ jobs:
66
run:
77
name: Run
88
runs-on: ubuntu-latest
9+
strategy:
10+
matrix:
11+
features: [--all-features, --no-default-features]
912
steps:
1013
- name: Install Rust stable
1114
uses: actions-rs/toolchain@v1
@@ -22,16 +25,16 @@ jobs:
2225
uses: actions-rs/cargo@v1
2326
with:
2427
command: build
25-
args: --workspace
28+
args: --workspace ${{ matrix.features }}
2629

2730
- name: Lint
2831
uses: actions-rs/cargo@v1
2932
with:
3033
command: clippy
31-
args: --workspace -- -D warnings
34+
args: --workspace ${{ matrix.features }} -- -D warnings
3235

3336
- name: Test
3437
uses: actions-rs/cargo@v1
3538
with:
3639
command: test
37-
args: --workspace
40+
args: --workspace ${{ matrix.features }}

Cargo.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@ repository = "https://github.com/mlafeldt/codebreaker-rs"
99
documentation = "https://docs.rs/codebreaker/"
1010
homepage = "https://crates.io/crates/codebreaker"
1111
keywords = ["codebreaker", "ps2", "gamehacking", "homebrew"]
12-
categories = ["algorithms", "cryptography"]
12+
categories = ["algorithms", "cryptography", "no-std"]
1313
edition = "2018"
1414

1515
[lib]
1616
name = "codebreaker"
1717

1818
[dependencies]
19-
num-bigint = "^0.2.4"
19+
num-bigint = "0.3"
20+
21+
[features]
22+
default = ["std"]
23+
std = ["num-bigint/std"]

src/cb1.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ const SEEDS: [[u32; 16]; 3] = [
9898
mod tests {
9999
use super::*;
100100
use crate::code;
101+
use crate::std_alloc::Vec;
101102

102103
struct Test {
103104
decrypted: &'static str,

src/cb7.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
//! Encrypt and decrypt cheat codes for CodeBreaker PS2 v7+.
22
33
use crate::rc4::Rc4;
4+
use crate::std_alloc::Vec;
45

5-
use std::fmt;
6-
use std::mem::size_of;
7-
use std::slice;
6+
use core::fmt;
7+
use core::mem::size_of;
8+
use core::slice;
89

910
/// A processor for CB v7+ codes.
1011
#[derive(Clone, Copy)]
@@ -466,6 +467,7 @@ const SEEDS: [[u8; 256]; 5] = [
466467
mod tests {
467468
use super::*;
468469
use crate::code;
470+
use crate::std_alloc::Vec;
469471

470472
fn mul_tests() -> Vec<(u32, u32, u32)> {
471473
vec![

src/lib.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,30 @@
3030
#![deny(clippy::all, clippy::nursery)]
3131
#![deny(nonstandard_style, rust_2018_idioms)]
3232
#![deny(missing_docs, missing_debug_implementations)]
33+
#![no_std]
34+
35+
#[cfg(feature = "std")]
36+
#[macro_use]
37+
extern crate std;
38+
39+
#[cfg(feature = "std")]
40+
mod std_alloc {
41+
#[cfg(test)]
42+
pub use std::string::String;
43+
pub use std::vec::Vec;
44+
}
45+
46+
#[cfg(not(feature = "std"))]
47+
#[allow(unused_imports)]
48+
#[macro_use]
49+
extern crate alloc;
50+
51+
#[cfg(not(feature = "std"))]
52+
mod std_alloc {
53+
#[cfg(test)]
54+
pub use alloc::string::String;
55+
pub use alloc::vec::Vec;
56+
}
3357

3458
pub mod cb1;
3559
pub mod cb7;
@@ -286,6 +310,7 @@ fn num_code_lines(addr: u32) -> usize {
286310
#[cfg(test)]
287311
mod tests {
288312
use super::*;
313+
use crate::std_alloc::Vec;
289314

290315
struct Test {
291316
cb: Codebreaker,
@@ -491,15 +516,17 @@ mod tests {
491516

492517
#[cfg(test)]
493518
mod code {
494-
pub(crate) fn parse(line: &str) -> (u32, u32) {
519+
use crate::std_alloc::{String, Vec};
520+
521+
pub fn parse(line: &str) -> (u32, u32) {
495522
let code: Vec<u32> = line
496523
.splitn(2, ' ')
497524
.map(|v| u32::from_str_radix(v, 16).unwrap())
498525
.collect();
499526
(code[0], code[1])
500527
}
501528

502-
pub(crate) fn format(code: (u32, u32)) -> String {
529+
pub fn format(code: (u32, u32)) -> String {
503530
format!("{:08X} {:08X}", code.0, code.1)
504531
}
505532
}

src/rc4.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ impl Rc4 {
3838
#[cfg(test)]
3939
mod tests {
4040
use super::*;
41+
use crate::std_alloc::Vec;
4142

4243
struct Test {
4344
key: &'static str,

0 commit comments

Comments
 (0)