From 1130cfb3f18e2ea26b60ecfbcc8c02775cd793db Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sat, 22 Jun 2024 22:27:17 +0200 Subject: [PATCH] Make this crate no_std MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- .github/workflows/ci.yml | 34 +++++++++++++++++++++++++++++++--- Cargo.toml | 3 +++ src/lib.rs | 15 +++++++++++++-- src/tables.rs | 4 ++-- 4 files changed, 49 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 541d58a..8620860 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -47,14 +47,14 @@ jobs: key: clippy-target-${{ runner.os }}-${{ steps.rust-version.outputs.version }}-${{ hashFiles('Cargo.lock') }}y - run: cargo clippy --all --all-targets - test: - name: test + test-no_std: + name: test no_std on Rust 1.81.0 runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - uses: sfackler/actions/rustup@master with: - version: 1.56.0 + version: 1.81.0 - run: echo "version=$(rustc --version)" >> $GITHUB_OUTPUT id: rust-version - uses: actions/cache@v3 @@ -74,3 +74,31 @@ jobs: path: target key: test-target-${{ runner.os }}-${{ steps.rust-version.outputs.version }}-${{ hashFiles('Cargo.lock') }}y - run: cargo test --all + + test-std: + name: test std on Rust 1.56.0 + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: sfackler/actions/rustup@master + with: + version: 1.56.0 + - run: echo "version=$(rustc --version)" >> $GITHUB_OUTPUT + id: rust-version + - uses: actions/cache@v3 + with: + path: ~/.cargo/registry/index + key: index-${{ runner.os }}-${{ github.run_number }} + restore-keys: | + index-${{ runner.os }}- + - run: cargo generate-lockfile + - uses: actions/cache@v3 + with: + path: ~/.cargo/registry/cache + key: registry-${{ runner.os }}-${{ steps.rust-version.outputs.version }}-${{ hashFiles('Cargo.lock') }} + - run: cargo fetch + - uses: actions/cache@v3 + with: + path: target + key: test-target-${{ runner.os }}-${{ steps.rust-version.outputs.version }}-${{ hashFiles('Cargo.lock') }}y + - run: cargo test --all --features=std diff --git a/Cargo.toml b/Cargo.toml index e7223e5..e255634 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,3 +11,6 @@ readme = "README.md" unicode-bidi = "0.3" unicode-normalization = "0.1" unicode-properties = "0.1.1" + +[features] +std = [] diff --git a/src/lib.rs b/src/lib.rs index 7027761..3e524a5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,13 +1,19 @@ //! An implementation of the "stringprep" algorithm defined in [RFC 3454][]. //! //! [RFC 3454]: https://tools.ietf.org/html/rfc3454 +#![no_std] #![warn(missing_docs)] +extern crate alloc; extern crate unicode_bidi; extern crate unicode_normalization; extern crate unicode_properties; -use std::borrow::Cow; -use std::fmt; +#[cfg(feature = "std")] +extern crate std; + +use alloc::borrow::Cow; +use alloc::string::String; +use core::fmt; use unicode_normalization::UnicodeNormalization; use unicode_properties::{GeneralCategoryGroup, UnicodeGeneralCategory}; @@ -44,7 +50,12 @@ impl fmt::Display for Error { } } +// This is only needed for Rust versions older than 1.81.0, before core::error::Error got +// stabilized. +#[cfg(feature = "std")] impl std::error::Error for Error {} +#[cfg(not(feature = "std"))] +impl core::error::Error for Error {} /// Prepares a string with the SASLprep profile of the stringprep algorithm. /// diff --git a/src/tables.rs b/src/tables.rs index 3eccb35..ecee304 100644 --- a/src/tables.rs +++ b/src/tables.rs @@ -1,6 +1,6 @@ //! Character Tables -use std::cmp::Ordering; -use std::str::Chars; +use core::cmp::Ordering; +use core::str::Chars; use unicode_bidi::{bidi_class, BidiClass}; use unicode_properties::{GeneralCategoryGroup, UnicodeGeneralCategory};