Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add no_std support #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ keywords = ["compression", "deflate", "macro", "include", "assets"]
[dependencies]
include-flate-codegen-exports = { version = "0.1.4", path = "codegen-exports" }
lazy_static = "1.3"
libflate = "1.0.0"
libflate = { version = "2.0.0", default-features = false }
core2 = { version = "0.4", default-features = false, features = ["alloc"] }

[badges]
travis-ci = {repository = "SOF3/include-flate"}

[features]
default = ["std"]
stable = ["include-flate-codegen-exports/stable"]
std = ["include-flate-codegen-exports/std", "libflate/std", "core2/std"]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please preserve the trailing newlines

1 change: 1 addition & 0 deletions codegen-exports/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ proc-macro-hack = "0.5.9"

[features]
stable = ["include-flate-codegen/stable"]
std = []
16 changes: 16 additions & 0 deletions codegen-exports/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
#![cfg_attr(not(any(test, feature = "std")), no_std)]

extern crate alloc;

#[cfg_attr(feature = "stable", proc_macro_hack::proc_macro_hack)]
pub use include_flate_codegen::deflate_file;

#[cfg_attr(feature = "stable", proc_macro_hack::proc_macro_hack)]
pub use include_flate_codegen::deflate_utf8_file;

#[cfg(feature = "std")]
pub type String = ::std::string::String;

#[cfg(not(feature = "std"))]
pub type String = ::alloc::string::String;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

::std::string::String is an alias of ::alloc::string::String, why do we need to differentiate them?


#[cfg(feature = "std")]
pub type Vec<T> = ::std::vec::Vec<T>;

#[cfg(not(feature = "std"))]
pub type Vec<T> = ::alloc::vec::Vec<T>;
18 changes: 11 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
//! which might be undesirable if the data are too large.
//! An actual installer is still required if the binary involves too many resources that do not need to be kept in RAM all time.

#![cfg_attr(not(any(test, feature = "std")), no_std)]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need a feature gate for this? #![no_std] should be usable in binaries compiled with std as well.


extern crate alloc;

use libflate::deflate;

/// The low-level macros used by this crate.
Expand Down Expand Up @@ -99,7 +103,7 @@ macro_rules! flate {

$crate::lazy_static! {
$(#[$meta])*
$(pub $(($($vis)+))?)? static ref $name: ::std::vec::Vec<u8> = $crate::decode($crate::codegen::deflate_file!($path));
$(pub $(($($vis)+))?)? static ref $name: $crate::codegen::Vec<u8> = $crate::decode($crate::codegen::deflate_file!($path));
}
};
($(#[$meta:meta])*
Expand All @@ -109,25 +113,25 @@ macro_rules! flate {

$crate::lazy_static! {
$(#[$meta])*
$(pub $(($($vis)+))?)? static ref $name: ::std::string::String = $crate::decode_string($crate::codegen::deflate_utf8_file!($path));
$(pub $(($($vis)+))?)? static ref $name: $crate::codegen::String = $crate::decode_string($crate::codegen::deflate_utf8_file!($path));
}
};
}

#[doc(hidden)]
pub fn decode(bytes: &[u8]) -> Vec<u8> {
use std::io::{Cursor, Read};
pub fn decode(bytes: &[u8]) -> codegen::Vec<u8> {
use core2::io::{Cursor, Read};

let mut dec = deflate::Decoder::new(Cursor::new(bytes));
let mut ret = Vec::new();
let mut ret = codegen::Vec::new();
dec.read_to_end(&mut ret)
.expect("Compiled DEFLATE buffer was corrupted");
ret
}

#[doc(hidden)]
pub fn decode_string(bytes: &[u8]) -> String {
pub fn decode_string(bytes: &[u8]) -> codegen::String {
// We should have checked for utf8 correctness in encode_utf8_file!
String::from_utf8(decode(bytes))
codegen::String::from_utf8(decode(bytes))
.expect("flate_str has malformed UTF-8 despite checked at compile time")
}
Loading