diff --git a/Cargo.lock b/Cargo.lock index 6dd3d8b..5b06366 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,4 +4,4 @@ version = 4 [[package]] name = "boilerext" -version = "0.1.0" +version = "0.1.1" diff --git a/Cargo.toml b/Cargo.toml index 156ca6a..e37e501 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "boilerext" description = "Rlib with useful boilerplate extension traits for QoL" -version = "0.1.0" +version = "0.1.1" edition = "2024" license = "MIT" repository = "https://git.javalsai.tuxcord.net/rust/boilerext" @@ -9,7 +9,8 @@ keywords = ["ext", "trait", "extension-trait"] categories = ["rust-patterns"] [features] -default = [] +default = ["std"] +std = [] [lints.clippy] cargo = { level = "warn", priority = -1 } diff --git a/README.md b/README.md index 156d88e..f53ca9b 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,8 @@ Rlib with useful boilerplate extension traits for QoL. +Support for `no_std` environments with most extensions preserved. + Aims to extend methods that have negligible performance cost, which only reasonable barrier is their lenthyness or unintuitiveness. @@ -14,6 +16,12 @@ wasn't there. Documentation is not hosted publicly for now, clone and run `cargo doc` to generate it. +# Installation + +```sh +cargo add --git https://git.javalsai.tuxcord.net/rust/boilerext +``` + # Development This project uses cargo test/miri test/doc/fmt/check/clippy, codespell, prettier diff --git a/scripts/check.sh b/scripts/check.sh index 1c01309..84212a1 100755 --- a/scripts/check.sh +++ b/scripts/check.sh @@ -20,8 +20,11 @@ errprint() { set -x cargo check -q + cargo check -q --no-default-features cargo clippy -q + cargo clippy -q --no-default-features cargo test -q + cargo test -q --no-default-features cargo doc -q cargo fmt --check codespell @@ -33,4 +36,5 @@ if [ "${1:-}" == "extra" ]; then ( set -x cargo +nightly miri test -q + cargo +nightly miri test -q --no-default-features ) fi diff --git a/src/lib.rs b/src/lib.rs index be4fe1b..7ca10dc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,8 @@ #![doc = include_str!("../README.md")] +#![no_std] + +#[cfg(feature = "std")] +extern crate std; pub mod prelude { //! Prelude with extended method names that should never collide. @@ -6,7 +10,7 @@ pub mod prelude { //! Trait identifiers are not exported, only the implementation methods are. //! //! ``` - //! # use std::ffi::CStr; + //! # use core::ffi::CStr; //! use boilerext::prelude::*; //! CStr::split_ch; //! ``` @@ -19,6 +23,7 @@ pub mod prelude { pub use crate::{cstr::CStrSplit as _, slice::RotateInsertExt as _, slice::SplitOn as _}; } +#[cfg(feature = "std")] pub mod unix_prelude { //! Analogous to [`crate::prelude`] but only for traits exclusive to unix-based systems. @@ -26,7 +31,7 @@ pub mod unix_prelude { } pub mod slice { - use std::borrow::Borrow; + use core::borrow::Borrow; /// Rotates the slice like `rotate_right` while inserting a given value and returning the /// popped-off elements. @@ -79,7 +84,7 @@ pub mod slice { // swaps `insert` with the rotated part of `self` such that `insert` also becomes the // return slice for idx in 0..N { - std::mem::swap(&mut self[idx], &mut insert[idx]); + core::mem::swap(&mut self[idx], &mut insert[idx]); } Some(insert) @@ -139,18 +144,19 @@ pub mod slice { } pub mod cstr { - use std::{ - ffi::{CStr, OsStr}, - num::NonZeroU8, - }; + use core::{ffi::CStr, num::NonZeroU8}; + #[cfg(feature = "std")] + use std::ffi::OsStr; use crate::slice::SplitOn; /// Implements transparent `as_*` methods for unix systems. + #[cfg(feature = "std")] pub trait UnixCStrAs { fn as_os_str(&self) -> &OsStr; } + #[cfg(feature = "std")] impl UnixCStrAs for CStr { fn as_os_str(&self) -> &OsStr { use std::os::unix::ffi::OsStrExt as _; @@ -163,7 +169,7 @@ pub mod cstr { /// # Examples /// /// ``` - /// # use std::num::NonZeroU8; + /// # use core::num::NonZeroU8; /// # use boilerext::cstr::CStrSplit; /// let string = c"abcde"; /// @@ -189,13 +195,16 @@ pub mod cstr { } pub mod str { + #[cfg(feature = "std")] use std::ffi::OsStr; /// Implements transparent `as_*` methods for unix systems. + #[cfg(feature = "std")] pub trait UnixStrAs { fn as_os_str(&self) -> &OsStr; } + #[cfg(feature = "std")] impl UnixStrAs for str { fn as_os_str(&self) -> &OsStr { use std::os::unix::ffi::OsStrExt as _;