Skip to content

Commit 8d6d2d1

Browse files
committed
Make this crate no_std
This allows users such as the jid crate to actually be no_std as well. core::error::Error got stabilized back in Rust 1.81.0, but in order to support older Rust versions we added a std feature.
1 parent 97e2588 commit 8d6d2d1

File tree

4 files changed

+49
-7
lines changed

4 files changed

+49
-7
lines changed

.github/workflows/ci.yml

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ jobs:
4747
key: clippy-target-${{ runner.os }}-${{ steps.rust-version.outputs.version }}-${{ hashFiles('Cargo.lock') }}y
4848
- run: cargo clippy --all --all-targets
4949

50-
test:
51-
name: test
50+
test-no_std:
51+
name: test-no_std
5252
runs-on: ubuntu-latest
5353
steps:
5454
- uses: actions/checkout@v3
5555
- uses: sfackler/actions/rustup@master
5656
with:
57-
version: 1.56.0
57+
version: 1.81.0
5858
- run: echo "version=$(rustc --version)" >> $GITHUB_OUTPUT
5959
id: rust-version
6060
- uses: actions/cache@v3
@@ -74,3 +74,31 @@ jobs:
7474
path: target
7575
key: test-target-${{ runner.os }}-${{ steps.rust-version.outputs.version }}-${{ hashFiles('Cargo.lock') }}y
7676
- run: cargo test --all
77+
78+
test-std:
79+
name: test-std
80+
runs-on: ubuntu-latest
81+
steps:
82+
- uses: actions/checkout@v3
83+
- uses: sfackler/actions/rustup@master
84+
with:
85+
version: 1.56.0
86+
- run: echo "version=$(rustc --version)" >> $GITHUB_OUTPUT
87+
id: rust-version
88+
- uses: actions/cache@v3
89+
with:
90+
path: ~/.cargo/registry/index
91+
key: index-${{ runner.os }}-${{ github.run_number }}
92+
restore-keys: |
93+
index-${{ runner.os }}-
94+
- run: cargo generate-lockfile
95+
- uses: actions/cache@v3
96+
with:
97+
path: ~/.cargo/registry/cache
98+
key: registry-${{ runner.os }}-${{ steps.rust-version.outputs.version }}-${{ hashFiles('Cargo.lock') }}
99+
- run: cargo fetch
100+
- uses: actions/cache@v3
101+
with:
102+
path: target
103+
key: test-target-${{ runner.os }}-${{ steps.rust-version.outputs.version }}-${{ hashFiles('Cargo.lock') }}y
104+
- run: cargo test --all --features=std

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ readme = "README.md"
1111
unicode-bidi = "0.3"
1212
unicode-normalization = "0.1"
1313
unicode-properties = "0.1.1"
14+
15+
[features]
16+
std = []

src/lib.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
//! An implementation of the "stringprep" algorithm defined in [RFC 3454][].
22
//!
33
//! [RFC 3454]: https://tools.ietf.org/html/rfc3454
4+
#![no_std]
45
#![warn(missing_docs)]
6+
extern crate alloc;
57
extern crate unicode_bidi;
68
extern crate unicode_normalization;
79
extern crate unicode_properties;
810

9-
use std::borrow::Cow;
10-
use std::fmt;
11+
#[cfg(feature = "std")]
12+
extern crate std;
13+
14+
use alloc::borrow::Cow;
15+
use alloc::string::String;
16+
use core::fmt;
1117
use unicode_normalization::UnicodeNormalization;
1218
use unicode_properties::{GeneralCategoryGroup, UnicodeGeneralCategory};
1319

@@ -44,7 +50,12 @@ impl fmt::Display for Error {
4450
}
4551
}
4652

53+
// This is only needed for Rust versions older than 1.81.0, before core::error::Error got
54+
// stabilized.
55+
#[cfg(feature = "std")]
4756
impl std::error::Error for Error {}
57+
#[cfg(not(feature = "std"))]
58+
impl core::error::Error for Error {}
4859

4960
/// Prepares a string with the SASLprep profile of the stringprep algorithm.
5061
///

src/tables.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Character Tables
2-
use std::cmp::Ordering;
3-
use std::str::Chars;
2+
use core::cmp::Ordering;
3+
use core::str::Chars;
44
use unicode_bidi::{bidi_class, BidiClass};
55
use unicode_properties::{GeneralCategoryGroup, UnicodeGeneralCategory};
66

0 commit comments

Comments
 (0)