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}";
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`:
682684#![ no_std]
683685#![ cfg_attr( feature = "simd-accel" , feature( stdsimd, core_intrinsics) ) ]
684686
687+ #[ cfg( feature = "alloc" ) ]
685688#[ cfg_attr( test, macro_use) ]
686689extern crate alloc;
690+
687691extern crate core;
688692#[ macro_use]
689693extern crate cfg_if;
@@ -723,7 +727,7 @@ mod macros;
723727) ) ]
724728mod simd_funcs;
725729
726- #[ cfg( test) ]
730+ #[ cfg( all ( test, feature = "alloc" ) ) ]
727731mod testing;
728732
729733mod big5;
@@ -750,8 +754,11 @@ use crate::ascii::iso_2022_jp_ascii_valid_up_to;
750754use crate :: utf_8:: utf8_valid_up_to;
751755use crate :: variant:: * ;
752756
757+ #[ cfg( feature = "alloc" ) ]
753758use alloc:: borrow:: Cow ;
759+ #[ cfg( feature = "alloc" ) ]
754760use alloc:: string:: String ;
761+ #[ cfg( feature = "alloc" ) ]
755762use alloc:: vec:: Vec ;
756763use core:: cmp:: Ordering ;
757764use 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) ]
48834910fn 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) ]
48884916fn 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) ]
49114939mod test_labels_names;
49124940
4913- #[ cfg( test) ]
4941+ #[ cfg( all ( test, feature = "alloc" ) ) ]
49144942mod tests {
49154943 use super :: * ;
49164944 use alloc:: borrow:: Cow ;
0 commit comments