Skip to content

Remove stable feature and make macro no-std #38

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

Merged
merged 1 commit into from
Oct 31, 2020
Merged
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
14 changes: 3 additions & 11 deletions lib/macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,14 @@ version = "0.1.8"
authors = ["Julien Cretin <[email protected]>"]
license = "MIT"
edition = "2018"
keywords = ["data-encoding", "macro", "static", "const", "compile-time"]
categories = ["encoding"]
keywords = ["no_std", "base64", "base32", "hex", "macro"]
categories = ["encoding", "no-std"]
readme = "README.md"
repository = "https://github.com/ia0/data-encoding"
documentation = "https://docs.rs/data-encoding-macro"
description = "Macros for data-encoding"
include = ["Cargo.toml", "LICENSE", "README.md", "src/lib.rs"]

[package.metadata.docs.rs]
no-default-features = true

[features]
default = ["stable"]
stable = ["data-encoding-macro-internal/stable", "proc-macro-hack"]

[dependencies]
data-encoding = { version = "2.3", path = "..", default-features = false, features = ["alloc"] }
data-encoding = { version = "2.3", path = "..", default-features = false }
data-encoding-macro-internal = { version = "0.1.8", path = "internal" }
proc-macro-hack = { version = "0.5", optional = true }
8 changes: 1 addition & 7 deletions lib/macro/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ encodings to be used with the [data-encoding] crate.
If you were familiar with the [binary_macros] crate, this library is actually
[inspired][binary_macros_issue] from it.

If you use a nightly compiler, you may disable the "stable" feature:

```
data-encoding-macro = { version = "0.1", default-features = false }
```

### Examples

You can define a compile-time byte slice or array (using the `hexlower` or
Expand All @@ -20,7 +14,7 @@ You can define a compile-time byte slice or array (using the `hexlower` or
```rust
const HELLO: &'static [u8] = &hexlower!("68656c6c6f");
const FOOBAR: &'static [u8] = &base64!("Zm9vYmFy");
// In nightly, it is possible to define an array instead of a slice:
// It is possible to define an array instead of a slice:
hexlower_array!("const HELLO" = "68656c6c6f");
base64_array!("const FOOBAR" = "Zm9vYmFy");
```
Expand Down
6 changes: 1 addition & 5 deletions lib/macro/internal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ include = ["Cargo.toml", "LICENSE", "README.md", "src/lib.rs"]
[lib]
proc-macro = true

[features]
stable = ["proc-macro-hack"]

[dependencies]
data-encoding = { version = "2.3", path = "../..", default-features = false, features = ["alloc"] }
proc-macro-hack = { version = "0.5", optional = true }
data-encoding = { version = "2.3", path = "../.." }
syn = "1"
16 changes: 2 additions & 14 deletions lib/macro/internal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,8 @@

#![warn(unused_results)]

extern crate proc_macro;
#[cfg(feature = "stable")]
extern crate proc_macro_hack;
extern crate syn;

extern crate data_encoding;

use proc_macro::token_stream::IntoIter;
use proc_macro::{TokenStream, TokenTree};
#[cfg(feature = "stable")]
use proc_macro_hack::proc_macro_hack;
use std::collections::HashMap;

use data_encoding::{BitOrder, Encoding, Specification, Translate, Wrap};
Expand Down Expand Up @@ -150,8 +141,7 @@ fn check_empty<T>(hash_map: HashMap<String, T>) {
}
}

#[cfg_attr(feature = "stable", proc_macro_hack)]
#[cfg_attr(not(feature = "stable"), proc_macro)]
#[proc_macro]
#[doc(hidden)]
pub fn internal_new_encoding(input: TokenStream) -> TokenStream {
let mut hash_map = parse_map(input.into_iter());
Expand All @@ -160,7 +150,6 @@ pub fn internal_new_encoding(input: TokenStream) -> TokenStream {
format!("{:?}", encoding.internal_implementation()).parse().unwrap()
}

#[cfg(not(feature = "stable"))]
#[proc_macro]
#[doc(hidden)]
pub fn internal_decode_array(input: TokenStream) -> TokenStream {
Expand All @@ -175,8 +164,7 @@ pub fn internal_decode_array(input: TokenStream) -> TokenStream {
format!("{}: [u8; {}] = {:?};", name, output.len(), output).parse().unwrap()
}

#[cfg_attr(feature = "stable", proc_macro_hack)]
#[cfg_attr(not(feature = "stable"), proc_macro)]
#[proc_macro]
#[doc(hidden)]
pub fn internal_decode_slice(input: TokenStream) -> TokenStream {
let mut hash_map = parse_map(input.into_iter());
Expand Down
168 changes: 17 additions & 151 deletions lib/macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,57 +8,33 @@
//! If you were familiar with the [binary_macros] crate, this library is
//! actually [inspired][binary_macros_issue] from it.
//!
//! If you use a nightly compiler, you may disable the "stable" feature:
//!
//! ```text
//! data-encoding-macro = { version = "0.1", default-features = false }
//! ```
//!
//! This library does not support no-std yet because it depends on a proc-macro library that depends
//! on data-encoding with std and cargo propagates that feature although it's a build dependency.
//! See https://github.com/rust-lang/cargo/issues/5730 for more information.
//!
//! # Examples
//!
//! You can define a compile-time byte slice from an encoded string literal:
//!
//! ```rust
//! # #![cfg_attr(not(feature = "stable"), feature(proc_macro_hygiene))]
//! #[macro_use]
//! extern crate data_encoding_macro;
//!
//! const HELLO_SLICE: &'static [u8] = &hexlower!("68656c6c6f");
//! const FOOBAR_SLICE: &'static [u8] = &base64!("Zm9vYmFy");
//! const HELLO_SLICE: &'static [u8] = &data_encoding_macro::hexlower!("68656c6c6f");
//! const FOOBAR_SLICE: &'static [u8] = &data_encoding_macro::base64!("Zm9vYmFy");
//! # fn main() {}
//! ```
//!
//! When you disable the "stable" feature (and use a nightly compiler), you can
//! also define a compile-time byte array from an encoded string literal:
//! You can also define a compile-time byte array from an encoded string literal:
//!
//! ```rust
//! # #[macro_use] extern crate data_encoding_macro;
//! # #[cfg(not(feature = "stable"))]
//! hexlower_array!("const HELLO" = "68656c6c6f");
//! # #[cfg(not(feature = "stable"))]
//! base64_array!("const FOOBAR" = "Zm9vYmFy");
//! data_encoding_macro::hexlower_array!("const HELLO" = "68656c6c6f");
//! data_encoding_macro::base64_array!("const FOOBAR" = "Zm9vYmFy");
//! # fn main() {}
//! ```
//!
//! You can define a compile-time custom encoding from its specification:
//!
//! ```rust
//! # #![cfg_attr(not(feature = "stable"), feature(proc_macro_hygiene))]
//! extern crate data_encoding;
//! #[macro_use]
//! extern crate data_encoding_macro;
//! use data_encoding::Encoding;
//!
//! const HEX: Encoding = new_encoding! {
//! const HEX: data_encoding::Encoding = data_encoding_macro::new_encoding! {
//! symbols: "0123456789abcdef",
//! translate_from: "ABCDEF",
//! translate_to: "abcdef",
//! };
//! const BASE64: Encoding = new_encoding! {
//! const BASE64: data_encoding::Encoding = data_encoding_macro::new_encoding! {
//! symbols: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
//! padding: '=',
//! };
Expand All @@ -72,32 +48,13 @@
//! [data-encoding]: https://crates.io/crates/data-encoding
//! [hexadecimal]: macro.hexlower_permissive.html

#![cfg_attr(not(feature = "stable"), feature(proc_macro_hygiene))]
#![warn(unused_results)]
#![no_std]
#![warn(unused_results)]

#[cfg(feature = "stable")]
extern crate proc_macro_hack;

extern crate data_encoding;
extern crate data_encoding_macro_internal;

#[cfg(feature = "stable")]
use proc_macro_hack::proc_macro_hack;

#[cfg(not(feature = "stable"))]
#[doc(hidden)]
pub use data_encoding_macro_internal::*;

#[cfg(feature = "stable")]
#[proc_macro_hack]
#[doc(hidden)]
pub use data_encoding_macro_internal::internal_new_encoding;

#[cfg(feature = "stable")]
#[proc_macro_hack]
#[doc(hidden)]
pub use data_encoding_macro_internal::internal_decode_slice;
pub use data_encoding_macro_internal::{
internal_decode_array, internal_decode_slice, internal_new_encoding,
};

/// Defines a compile-time byte array by decoding a string literal
///
Expand All @@ -110,10 +67,7 @@ pub use data_encoding_macro_internal::internal_decode_slice;
/// # Examples
///
/// ```rust
/// #[macro_use]
/// extern crate data_encoding_macro;
///
/// decode_array! {
/// data_encoding_macro::decode_array! {
/// name: "const OCTAL",
/// symbols: "01234567",
/// padding: '=',
Expand All @@ -123,7 +77,6 @@ pub use data_encoding_macro_internal::internal_decode_slice;
/// ```
///
/// [new_encoding]: macro.new_encoding.html
#[cfg(not(feature = "stable"))]
#[macro_export]
macro_rules! decode_array {
($($arg: tt)*) => {
Expand All @@ -141,11 +94,7 @@ macro_rules! decode_array {
/// # Examples
///
/// ```rust
/// # #![feature(proc_macro_hygiene)]
/// #[macro_use]
/// extern crate data_encoding_macro;
///
/// const OCTAL: &'static [u8] = &decode_slice! {
/// const OCTAL: &'static [u8] = &data_encoding_macro::decode_slice! {
/// symbols: "01234567",
/// padding: '=',
/// input: "237610==",
Expand All @@ -154,44 +103,13 @@ macro_rules! decode_array {
/// ```
///
/// [new_encoding]: macro.new_encoding.html
#[cfg(not(feature = "stable"))]
#[macro_export]
macro_rules! decode_slice {
($($arg: tt)*) => {
$crate::internal_decode_slice!($($arg)*)
};
}

/// Defines a compile-time byte slice by decoding a string literal
///
/// This macro takes a list of `key: value,` pairs (the last comma is required).
/// It takes the key-value pairs specifying the encoding to use to decode the
/// input (see [new_encoding] for the possible key-value pairs), the input
/// itself keyed by `input`, and the output keyed by `name`.
///
/// # Examples
///
/// ```rust
/// #[macro_use]
/// extern crate data_encoding_macro;
///
/// const OCTAL: &'static [u8] = &decode_slice! {
/// symbols: "01234567",
/// padding: '=',
/// input: "237610==",
/// };
/// # fn main() {}
/// ```
///
/// [new_encoding]: macro.new_encoding.html
#[cfg(feature = "stable")]
#[macro_export]
macro_rules! decode_slice {
($($arg: tt)*) => {
internal_decode_slice!($($arg)*)
};
}

/// Defines a compile-time custom encoding
///
/// This macro takes a list of `key: value,` pairs (the last comma is required).
Expand All @@ -215,57 +133,7 @@ macro_rules! decode_slice {
/// # Examples
///
/// ```rust
/// # #![feature(proc_macro_hygiene)]
/// extern crate data_encoding;
/// #[macro_use]
/// extern crate data_encoding_macro;
///
/// const HEX: data_encoding::Encoding = new_encoding! {
/// symbols: "0123456789abcdef",
/// ignore: " \r\t\n",
/// wrap_width: 32,
/// wrap_separator: "\n",
/// translate_from: "ABCDEF",
/// translate_to: "abcdef",
/// };
/// # fn main() {}
/// ```
#[cfg(not(feature = "stable"))]
#[macro_export]
macro_rules! new_encoding {
($($arg: tt)*) => {
::data_encoding::Encoding::internal_new(&$crate::internal_new_encoding!{ $($arg)* })
};
}

/// Defines a compile-time custom encoding
///
/// This macro takes a list of `key: value,` pairs (the last comma is required).
/// The possible key-value pairs are:
///
/// ```text
/// symbols: <string>, // e.g. "01234567"
/// padding: [None]|<char>, // e.g. '='
/// bit_order: [MostSignificantFirst]|LeastSignificantFirst,
/// check_trailing_bits: [true]|false,
/// ignore: [""]|<string>, // e.g. " \t\n"
/// wrap_width: [0]|<int>, // e.g. 76
/// wrap_separator: [""]|<string>, // e.g. "\r\n"
/// translate_from: [""]|<string>, // e.g. "ABCDEF"
/// translate_to: [""]|<string>, // e.g. "abcdef"
/// ```
///
/// Only `symbols` is required. Everything else is optional and defaults to the
/// value between square brackets.
///
/// # Examples
///
/// ```rust
/// extern crate data_encoding;
/// #[macro_use]
/// extern crate data_encoding_macro;
///
/// const HEX: data_encoding::Encoding = new_encoding! {
/// const HEX: data_encoding::Encoding = data_encoding_macro::new_encoding! {
/// symbols: "0123456789abcdef",
/// ignore: " \r\t\n",
/// wrap_width: 32,
Expand All @@ -275,27 +143,25 @@ macro_rules! new_encoding {
/// };
/// # fn main() {}
/// ```
#[cfg(feature = "stable")]
#[macro_export]
macro_rules! new_encoding {
($($arg: tt)*) => {
::data_encoding::Encoding::internal_new(&internal_new_encoding!{ $($arg)* })
data_encoding::Encoding::internal_new(&$crate::internal_new_encoding!{ $($arg)* })
};
}

macro_rules! make {
($base: ident $base_array: ident = $ref: ident; $($spec: tt)*) => {
#[cfg(not(feature = "stable"))]
#[macro_export]
macro_rules! $base_array {
($n: tt = $x: tt) => {
decode_array!(name: $n, input: $x, $($spec)*);
$crate::decode_array!(name: $n, input: $x, $($spec)*);
};
}
#[macro_export]
macro_rules! $base {
($x: tt) => {
decode_slice!(input: $x, $($spec)*)
$crate::decode_slice!(input: $x, $($spec)*)
};
}
#[test]
Expand Down
Loading