Skip to content

Commit

Permalink
Add std feature, allow crate to work in #![no_std] mode
Browse files Browse the repository at this point in the history
Disabling the `std` feature (by setting `default-features = false` for
this crate in the Cargo.toml of something requiring it) will enable the
crate to work without the standard library, assuming the presence of
the `alloc` crate and a working global allocator.

We've explicitly imported the individual `alloc` items required by this
crate, rather than using `#![feature(alloc_prelude)]` as `alloc_prelude`
requires Rust nightly, and doing it that way would be a breaking change
to the MSRV.

Tested with Cargo/rustc 1.53.0
  • Loading branch information
u1f408 committed Aug 26, 2021
1 parent 89fa2d1 commit 458d861
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ categories = [
"command-line-interface",
"parser-implementations"
]

[features]
std = []
default = ["std"]
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ specially, which I believe is more compliant.
The algorithms in this crate are oblivious to UTF-8 high bytes, so they iterate
over the bytes directly as a micro-optimization.

Disabling the `std` feature (which is enabled by default) will allow the crate
to work in `no_std` environments, where a the `alloc` crate, and a global
allocator, are available.

# LICENSE

The source code in this repository is Licensed under either of
Expand Down
16 changes: 14 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,25 @@
//!
//! The algorithms in this crate are oblivious to UTF-8 high bytes, so they iterate over the bytes
//! directly as a micro-optimization.
//!
//! Disabling the `std` feature (which is enabled by default) will allow the crate to work in
//! `no_std` environments, where a the `alloc` crate, and a global allocator, are available.

#![cfg_attr(not(feature = "std"), no_std)]

use std::borrow::Cow;
extern crate alloc;
use alloc::vec::Vec;
use alloc::borrow::Cow;
use alloc::string::String;
#[cfg(test)]
use alloc::vec;
#[cfg(test)]
use alloc::borrow::ToOwned;

/// An iterator that takes an input string and splits it into the words using the same syntax as
/// the POSIX shell.
pub struct Shlex<'a> {
in_iter: std::str::Bytes<'a>,
in_iter: core::str::Bytes<'a>,
/// The number of newlines read so far, plus one.
pub line_no: usize,
/// An input string is erroneous if it ends while inside a quotation or right after an
Expand Down

0 comments on commit 458d861

Please sign in to comment.