Skip to content

Commit cecda92

Browse files
committed
Make the parts that use an allocator optional.
1 parent 19efaf0 commit cecda92

File tree

16 files changed

+71
-29
lines changed

16 files changed

+71
-29
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ categories = ["text-processing", "encoding", "web-programming", "internationaliz
1616
travis-ci = { repository = "hsivonen/encoding_rs" }
1717

1818
[features]
19+
default = ["alloc"]
20+
alloc = []
1921
simd-accel = ["packed_simd", "packed_simd/into_bits"]
2022
less-slow-kanji-encode = []
2123
less-slow-big5-hanzi-encode = []

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,10 @@ crate provides that capability.
7777

7878
## `no_std` Environment
7979

80-
The crate works in a `no_std` environment assuming that `alloc` is present.
81-
The `alloc`-using part are on the outer edge of the crate, so if there is
82-
interest in using the crate in environments without `alloc` it would be
83-
feasible to add a way to turn off those parts of the API of this crate that
84-
use `Vec`/`String`/`Cow`.
80+
The crate works in a `no_std` environment. By default, the `alloc` feature,
81+
which assumes that an allocator is present is enabled. For a no-allocator
82+
environment, the default features (i.e. `alloc`) can be turned off. This
83+
makes the part of the API that returns `Vec`/`String`/`Cow` unavailable.
8584

8685
## Decoding Email
8786

@@ -432,6 +431,10 @@ To regenerate the generated code:
432431

433432
## Release Notes
434433

434+
### 0.8.29
435+
436+
* Make the parts that use an allocator optional.
437+
435438
### 0.8.28
436439

437440
* Fix error in Serde support introduced as part of `no_std` support.

src/ascii.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1508,7 +1508,7 @@ pub fn iso_2022_jp_ascii_valid_up_to(bytes: &[u8]) -> usize {
15081508
// Any copyright to the test code below this comment is dedicated to the
15091509
// Public Domain. http://creativecommons.org/publicdomain/zero/1.0/
15101510

1511-
#[cfg(test)]
1511+
#[cfg(all(test, feature = "alloc"))]
15121512
mod tests {
15131513
use super::*;
15141514
use alloc::vec::Vec;

src/big5.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ impl Big5Encoder {
263263
// Any copyright to the test code below this comment is dedicated to the
264264
// Public Domain. http://creativecommons.org/publicdomain/zero/1.0/
265265

266-
#[cfg(test)]
266+
#[cfg(all(test, feature = "alloc"))]
267267
mod tests {
268268
use super::super::testing::*;
269269
use super::super::*;

src/euc_jp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ impl EucJpEncoder {
346346
// Any copyright to the test code below this comment is dedicated to the
347347
// Public Domain. http://creativecommons.org/publicdomain/zero/1.0/
348348

349-
#[cfg(test)]
349+
#[cfg(all(test, feature = "alloc"))]
350350
mod tests {
351351
use super::super::testing::*;
352352
use super::super::*;

src/euc_kr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ impl EucKrEncoder {
362362
// Any copyright to the test code below this comment is dedicated to the
363363
// Public Domain. http://creativecommons.org/publicdomain/zero/1.0/
364364

365-
#[cfg(test)]
365+
#[cfg(all(test, feature = "alloc"))]
366366
mod tests {
367367
use super::super::testing::*;
368368
use super::super::*;

src/gb18030.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ impl Gb18030Encoder {
571571
// Any copyright to the test code below this comment is dedicated to the
572572
// Public Domain. http://creativecommons.org/publicdomain/zero/1.0/
573573

574-
#[cfg(test)]
574+
#[cfg(all(test, feature = "alloc"))]
575575
mod tests {
576576
use super::super::testing::*;
577577
use super::super::*;

src/iso_2022_jp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ impl Iso2022JpEncoder {
754754
// Any copyright to the test code below this comment is dedicated to the
755755
// Public Domain. http://creativecommons.org/publicdomain/zero/1.0/
756756

757-
#[cfg(test)]
757+
#[cfg(all(test, feature = "alloc"))]
758758
mod tests {
759759
use super::super::testing::*;
760760
use super::super::*;

src/lib.rs

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
//! Decode using the non-streaming API:
6060
//!
6161
//! ```
62+
//! #[cfg(feature = "alloc")] {
6263
//! use encoding_rs::*;
6364
//!
6465
//! let expectation = "\u{30CF}\u{30ED}\u{30FC}\u{30FB}\u{30EF}\u{30FC}\u{30EB}\u{30C9}";
@@ -68,6 +69,7 @@
6869
//! assert_eq!(&cow[..], expectation);
6970
//! assert_eq!(encoding_used, SHIFT_JIS);
7071
//! assert!(!had_errors);
72+
//! }
7173
//! ```
7274
//!
7375
//! Decode using the streaming API with minimal `unsafe`:
@@ -682,8 +684,10 @@
682684
#![no_std]
683685
#![cfg_attr(feature = "simd-accel", feature(stdsimd, core_intrinsics))]
684686

687+
#[cfg(feature = "alloc")]
685688
#[cfg_attr(test, macro_use)]
686689
extern crate alloc;
690+
687691
extern crate core;
688692
#[macro_use]
689693
extern crate cfg_if;
@@ -723,7 +727,7 @@ mod macros;
723727
))]
724728
mod simd_funcs;
725729

726-
#[cfg(test)]
730+
#[cfg(all(test, feature = "alloc"))]
727731
mod testing;
728732

729733
mod big5;
@@ -750,8 +754,11 @@ use crate::ascii::iso_2022_jp_ascii_valid_up_to;
750754
use crate::utf_8::utf8_valid_up_to;
751755
use crate::variant::*;
752756

757+
#[cfg(feature = "alloc")]
753758
use alloc::borrow::Cow;
759+
#[cfg(feature = "alloc")]
754760
use alloc::string::String;
761+
#[cfg(feature = "alloc")]
755762
use alloc::vec::Vec;
756763
use core::cmp::Ordering;
757764
use core::hash::Hash;
@@ -2894,6 +2901,7 @@ impl Encoding {
28942901

28952902
/// Checks whether the bytes 0x00...0x7F map mostly to the characters
28962903
/// U+0000...U+007F and vice versa.
2904+
#[cfg(feature = "alloc")]
28972905
#[inline]
28982906
fn is_potentially_borrowable(&'static self) -> bool {
28992907
!(self == REPLACEMENT || self == UTF_16BE || self == UTF_16LE)
@@ -2945,7 +2953,9 @@ impl Encoding {
29452953
/// If the size calculation for a heap-allocated backing buffer overflows
29462954
/// `usize`.
29472955
///
2948-
/// Available to Rust only.
2956+
/// Available to Rust only and only with the `alloc` feature enabled (enabled
2957+
/// by default).
2958+
#[cfg(feature = "alloc")]
29492959
#[inline]
29502960
pub fn decode<'a>(&'static self, bytes: &'a [u8]) -> (Cow<'a, str>, &'static Encoding, bool) {
29512961
let (encoding, without_bom) = match Encoding::for_bom(bytes) {
@@ -2988,7 +2998,9 @@ impl Encoding {
29882998
/// If the size calculation for a heap-allocated backing buffer overflows
29892999
/// `usize`.
29903000
///
2991-
/// Available to Rust only.
3001+
/// Available to Rust only and only with the `alloc` feature enabled (enabled
3002+
/// by default).
3003+
#[cfg(feature = "alloc")]
29923004
#[inline]
29933005
pub fn decode_with_bom_removal<'a>(&'static self, bytes: &'a [u8]) -> (Cow<'a, str>, bool) {
29943006
let without_bom = if self == UTF_8 && bytes.starts_with(b"\xEF\xBB\xBF") {
@@ -3035,7 +3047,9 @@ impl Encoding {
30353047
/// If the size calculation for a heap-allocated backing buffer overflows
30363048
/// `usize`.
30373049
///
3038-
/// Available to Rust only.
3050+
/// Available to Rust only and only with the `alloc` feature enabled (enabled
3051+
/// by default).
3052+
#[cfg(feature = "alloc")]
30393053
pub fn decode_without_bom_handling<'a>(&'static self, bytes: &'a [u8]) -> (Cow<'a, str>, bool) {
30403054
let (mut decoder, mut string, mut total_read) = if self.is_potentially_borrowable() {
30413055
let valid_up_to = if self == UTF_8 {
@@ -3130,7 +3144,9 @@ impl Encoding {
31303144
/// If the size calculation for a heap-allocated backing buffer overflows
31313145
/// `usize`.
31323146
///
3133-
/// Available to Rust only.
3147+
/// Available to Rust only and only with the `alloc` feature enabled (enabled
3148+
/// by default).
3149+
#[cfg(feature = "alloc")]
31343150
pub fn decode_without_bom_handling_and_without_replacement<'a>(
31353151
&'static self,
31363152
bytes: &'a [u8],
@@ -3225,7 +3241,9 @@ impl Encoding {
32253241
/// If the size calculation for a heap-allocated backing buffer overflows
32263242
/// `usize`.
32273243
///
3228-
/// Available to Rust only.
3244+
/// Available to Rust only and only with the `alloc` feature enabled (enabled
3245+
/// by default).
3246+
#[cfg(feature = "alloc")]
32293247
pub fn encode<'a>(&'static self, string: &'a str) -> (Cow<'a, [u8]>, &'static Encoding, bool) {
32303248
let output_encoding = self.output_encoding();
32313249
if output_encoding == UTF_8 {
@@ -3988,7 +4006,9 @@ impl Decoder {
39884006
/// See the documentation of the struct for documentation for `decode_*`
39894007
/// methods collectively.
39904008
///
3991-
/// Available to Rust only.
4009+
/// Available to Rust only and only with the `alloc` feature enabled (enabled
4010+
/// by default).
4011+
#[cfg(feature = "alloc")]
39924012
pub fn decode_to_string(
39934013
&mut self,
39944014
src: &[u8],
@@ -4076,7 +4096,9 @@ impl Decoder {
40764096
/// See the documentation of the struct for documentation for `decode_*`
40774097
/// methods collectively.
40784098
///
4079-
/// Available to Rust only.
4099+
/// Available to Rust only and only with the `alloc` feature enabled (enabled
4100+
/// by default).
4101+
#[cfg(feature = "alloc")]
40804102
pub fn decode_to_string_without_replacement(
40814103
&mut self,
40824104
src: &[u8],
@@ -4575,7 +4597,9 @@ impl Encoder {
45754597
/// See the documentation of the struct for documentation for `encode_*`
45764598
/// methods collectively.
45774599
///
4578-
/// Available to Rust only.
4600+
/// Available to Rust only and only with the `alloc` feature enabled (enabled
4601+
/// by default).
4602+
#[cfg(feature = "alloc")]
45794603
pub fn encode_from_utf8_to_vec(
45804604
&mut self,
45814605
src: &str,
@@ -4613,7 +4637,9 @@ impl Encoder {
46134637
/// See the documentation of the struct for documentation for `encode_*`
46144638
/// methods collectively.
46154639
///
4616-
/// Available to Rust only.
4640+
/// Available to Rust only and only with the `alloc` feature enabled (enabled
4641+
/// by default).
4642+
#[cfg(feature = "alloc")]
46174643
pub fn encode_from_utf8_to_vec_without_replacement(
46184644
&mut self,
46194645
src: &str,
@@ -4879,11 +4905,13 @@ fn checked_div(opt: Option<usize>, num: usize) -> Option<usize> {
48794905
}
48804906
}
48814907

4908+
#[cfg(feature = "alloc")]
48824909
#[inline(always)]
48834910
fn checked_next_power_of_two(opt: Option<usize>) -> Option<usize> {
48844911
opt.map(|n| n.next_power_of_two())
48854912
}
48864913

4914+
#[cfg(feature = "alloc")]
48874915
#[inline(always)]
48884916
fn checked_min(one: Option<usize>, other: Option<usize>) -> Option<usize> {
48894917
if let Some(a) = one {
@@ -4910,7 +4938,7 @@ struct Demo {
49104938
#[cfg(test)]
49114939
mod test_labels_names;
49124940

4913-
#[cfg(test)]
4941+
#[cfg(all(test, feature = "alloc"))]
49144942
mod tests {
49154943
use super::*;
49164944
use alloc::borrow::Cow;

src/mem.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@
2424
//! The FFI binding for this module are in the
2525
//! [encoding_c_mem crate](https://github.com/hsivonen/encoding_c_mem).
2626
27+
#[cfg(feature = "alloc")]
2728
use alloc::borrow::Cow;
29+
#[cfg(feature = "alloc")]
2830
use alloc::string::String;
31+
#[cfg(feature = "alloc")]
2932
use alloc::vec::Vec;
3033

3134
use super::in_inclusive_range16;
@@ -1988,6 +1991,9 @@ pub fn convert_utf16_to_latin1_lossy(src: &[u16], dst: &mut [u8]) {
19881991
///
19891992
/// Borrows if input is ASCII-only. Performs a single heap allocation
19901993
/// otherwise.
1994+
///
1995+
/// Only available if the `alloc` feature is enabled (enabled by default).
1996+
#[cfg(feature = "alloc")]
19911997
pub fn decode_latin1<'a>(bytes: &'a [u8]) -> Cow<'a, str> {
19921998
let up_to = ascii_valid_up_to(bytes);
19931999
// >= makes later things optimize better than ==
@@ -2022,6 +2028,9 @@ pub fn decode_latin1<'a>(bytes: &'a [u8]) -> Cow<'a, str> {
20222028
///
20232029
/// Borrows if input is ASCII-only. Performs a single heap allocation
20242030
/// otherwise.
2031+
///
2032+
/// Only available if the `alloc` feature is enabled (enabled by default).
2033+
#[cfg(feature = "alloc")]
20252034
pub fn encode_latin1_lossy<'a>(string: &'a str) -> Cow<'a, [u8]> {
20262035
let bytes = string.as_bytes();
20272036
let up_to = ascii_valid_up_to(bytes);
@@ -2155,7 +2164,7 @@ pub fn copy_basic_latin_to_ascii(src: &[u16], dst: &mut [u8]) -> usize {
21552164
// Any copyright to the test code below this comment is dedicated to the
21562165
// Public Domain. http://creativecommons.org/publicdomain/zero/1.0/
21572166

2158-
#[cfg(test)]
2167+
#[cfg(all(test, feature = "alloc"))]
21592168
mod tests {
21602169
use super::*;
21612170

0 commit comments

Comments
 (0)