Skip to content

Commit

Permalink
1. new release version 1.0.4
Browse files Browse the repository at this point in the history
2. updated dependencies
3. improved documentation
4. fixed minor bugs in code
5. removed previously deprecated code
6. renamed some examples
  • Loading branch information
denisandroid committed May 14, 2024
1 parent 2262dbc commit 92cb8ff
Show file tree
Hide file tree
Showing 18 changed files with 145 additions and 184 deletions.
11 changes: 6 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "include_tt"
version = "1.0.3"
version = "1.0.4"
edition = "2021"
authors = ["Denis Kotlyarov (Денис Котляров) <denis2005991@gmail.com>"]
repository = "https://github.com/clucompany/include_tt.git"
Expand All @@ -16,9 +16,10 @@ categories = ["development-tools", "development-tools::build-utils"]
proc-macro = true

[dependencies]
quote = "1.0.35"
proc-macro2 = "1.0.79"
quote = "1.0.36"
proc-macro2 = "1.0.82"

[dependencies.syn]
version = "2.0.57"
features = ["full"]
version = "2.0.63"
default-features = false
features = ["parsing"]
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ use std::fmt::Write;
&mut end_str,

"arg1: {}, arg2: {}",

// This file contains `a, b`.
#include!("./for_examples/full.tt")
#include!("./for_examples/full.tt") // this file contains `a, b`.
);
}

Expand All @@ -77,7 +75,7 @@ use std::fmt::Write;
{
// Loading a string from "full.tt" using include_tt! macro.
let str = include_tt!(
#include_str!("./for_examples/full.tt")
#include_str!("./for_examples/full.tt") // this file contains `a, b`.
);

// Asserting the result matches the expected output.
Expand All @@ -87,7 +85,7 @@ use std::fmt::Write;
{
// Loading a array from "full.tt" using include_tt! macro.
let array: &'static [u8; 4] = include_tt!(
#include_arr!("./for_examples/full.tt")
#include_arr!("./for_examples/full.tt") // this file contains `a, b`.
);

// Asserting the result matches the expected output.
Expand Down
9 changes: 6 additions & 3 deletions examples/custom_path.rs → examples/constmodule.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#[macro_export]
macro_rules! __test_custom_path {
[ @$n: ident($const_t: ident) : [$t1:tt $t2:tt $t3:tt $t4:tt $t5:tt $t6:tt]; ] => {
Expand All @@ -8,16 +7,20 @@ macro_rules! __test_custom_path {
pub mod ttest {
pub const a: usize = 0;
pub const b: usize = 10;

pub const $const_t: (usize, usize) = (#include!([$t1 $t2 $t3 $t4 $t5 $t6]));
}
}
};
}

fn main() {
// we created a module "ttest" and a constant "T" containing (a, b).
//
// if you need to change, for example, to (b,a) or substitute constant values,
// we will only change the contents of the file "for_examples/full.tt"!
__test_custom_path! {
@ttest(T): [for_examples / "full" . t 't'];
@ttest(T): [for_examples / "full" . t 't']; // this file contains "a, b", see "for_examples/full.tt"
}
assert_eq!(ttest::T, (0, 10));
}
50 changes: 25 additions & 25 deletions examples/full.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@

use std::fmt::Write;
use include_tt::include_tt;
use std::fmt::Write;

macro_rules! test2_rules {
[
[a, b]

$($tt:tt)*
] => {
println!("test2_rules: [a, b]");
Expand All @@ -15,26 +14,26 @@ macro_rules! test2_rules {
};
[
[c, d]

$($tt:tt)*
] => {
println!("test2_rules: [c, d]");
test2_rules! {
$($tt)*
}
};

[
a, b

$($tt:tt)*
] => {
println!("test2_rules: a, b");
test2_rules! {
$($tt)*
}
};

[$($tt:tt)+] => {
compile_error!(stringify!( $($tt)* ))
};
Expand All @@ -45,47 +44,48 @@ fn main() {
// Loading trees from a file and substituting them into a custom macro.
include_tt! {
test2_rules! {
[#include!("./for_examples/full.tt")]
[#include! { "./for_examples/full.tt"}]
[#include!("./for_examples/full.tt")] // this file contains `a, b`.
[#include! { "./for_examples/full.tt"}] // this file contains `a, b`.
}
test2_rules! {
#include!("./for_examples/full.tt")
#include!("./for_examples/full.tt") // this file contains `a, b`.
}

println!(
concat!(
"#",
#include_str!("./for_examples/full.tt"),
#include_str!("./for_examples/full.tt"), // this file contains `a, b`.
"#"
)
);
}

{ // Loading a string from a file.
let str = include_tt!(#include_str!("./for_examples/full.tt"));

{
// Loading a string from a file.
let str = include_tt!(#include_str!("./for_examples/full.tt")); // this file contains `a, b`.
assert_eq!(str, "a, b");
}

{ // Loading an array from a file.

{
// Loading an array from a file.
let array: &'static [u8; 4] = include_tt!(
#include_arr!("./for_examples/full.tt")
#include_arr!("./for_examples/full.tt") // this file contains `a, b`.
);
assert_eq!(array, b"a, b");
}

{ // Embedding compiler trees from a file in an arbitrary place of other macros.

{
// Embedding compiler trees from a file in an arbitrary place of other macros.
let a = 10;
let b = 20;

let mut end_str = String::new();
include_tt! {
let _e = write!(
&mut end_str,

"arg1: {}, arg2: {}",

// This file contains `a, b`.
#include!("./for_examples/full.tt")
#include!("./for_examples/full.tt") // this file contains `a, b`.
);
}
assert_eq!(end_str, "arg1: 10, arg2: 20");
Expand Down
19 changes: 11 additions & 8 deletions examples/easy.rs → examples/mrules.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@

use include_tt::include_tt;

macro_rules! test_rules {
[
$a:ident + $b:ident = $c:ident

$($tt:tt)*
] => {
let $c = $a + $b;

test_rules! {
$($tt)*
}
};
[
$a:ident - $b:ident = $c:ident

$($tt:tt)*
] => {
let $c = $a - $b;

test_rules! {
$($tt)*
}
Expand All @@ -37,10 +36,14 @@ fn main() {
let b = 20;

include_tt! {
// this macro only supports:
// a + b = n
// or
// a - b = n
test_rules! {
#include!("./for_examples/easy.tt")
#include!("./for_examples/mrules.tt") // this file contains "a + b = n", see "./for_examples/mrules.tt"
}
}
assert_eq!(n, a+b);
println!("n: {:?}", n); // << n :) $_$
assert_eq!(n, a + b);
println!("n: {:?}", n); // 30
}
File renamed without changes.
2 changes: 2 additions & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[toolchain]
channel = "stable"
2 changes: 2 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
tab_spaces = 4
hard_tabs = true
81 changes: 16 additions & 65 deletions src/exprs/literal.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

use std::{ops::Deref, fmt::{Debug, Display}, borrow::{Cow, Borrow}};
use std::{ops::Deref, fmt::{Debug, Display}, borrow::Borrow};
use proc_macro2::{Span, TokenStream as TokenStream2};
use crate::sg_err;

Expand Down Expand Up @@ -94,7 +94,7 @@ impl Borrow<ExprLit> for String {
impl ExprLit {
/// Creating `ExprLit` without clipping.
#[inline]
const fn __new<'a>(a: &'a str) -> &'a ExprLit {
const fn __new(a: &str) -> &ExprLit {
// It is safe, there is no other way than to "transmute", "box"
// to create a dimensionless structure.
unsafe { &*(a as *const _ as *const ExprLit) }
Expand All @@ -109,16 +109,16 @@ impl ExprLit {
/// Create an `ExprLit` from the expression `"test"` and return it.
#[allow(dead_code)]
#[inline]
pub fn try_new<'a>(a: &'a str) -> Result<&'a ExprLit, ExprLitTryNewErr> {
Self::try_new_fn(
pub fn try_new(a: &str) -> Result<&ExprLit, ExprLitTryNewErr> {
Self::try_new_with_fns(
a,
|ok| Ok(ok),
|e| Err(e)
Ok,
Err
)
}

/// Create an `ExprLit` from the expression `"test"` and return it.
pub fn try_new_fn<'a, R>(a: &'a str, next: impl FnOnce(&'a ExprLit) -> R, err: impl FnOnce(ExprLitTryNewErr) -> R) -> R {
pub fn try_new_with_fns<'a, R>(a: &'a str, next: impl FnOnce(&'a ExprLit) -> R, err: impl FnOnce(ExprLitTryNewErr) -> R) -> R {
let a_array = a.as_bytes();

let len = a_array.len();
Expand All @@ -128,9 +128,14 @@ impl ExprLit {
exp: 2,
});
}

debug_assert_eq!(a_array.get(0).is_some(), true);
debug_assert_eq!(a_array.get(len -1).is_some(), true);
debug_assert!({
#[allow(clippy::get_first)] // why?, this is done to be completely analogous to an unsafe function.
a_array.get(0).is_some()
});
debug_assert!({
#[allow(clippy::get_first)] // why?, this is done to be completely analogous to an unsafe function.
a_array.get(len -1).is_some()
});
/*
This is safe, the extra necessary checks are done in a separate `if` above.
*/
Expand All @@ -153,7 +158,7 @@ impl ExprLit {
}

next({
debug_assert_eq!(a.get(1..len-1).is_some(), true);
debug_assert!(a.get(1..len-1).is_some());

// It's safe, checks are done above (above `debug_assert`).
let str = unsafe {
Expand All @@ -163,59 +168,6 @@ impl ExprLit {
})
}

/// Create an `ExprLit` from the expression `"test"` and return it.
#[inline]
#[deprecated(since="1.0.2", note="please use `try_new` instead")]
pub fn try_new_search_and_autoreplaceshielding<'a>(a: &'a str) -> Result<Cow<'a, ExprLit>, ExprLitTryNewErr> {
#[allow(deprecated)]
Self::try_new_search_and_autoreplaceshielding_fn(
a,
|a| Ok(a),
|e| Err(e),
)
}

/// Create an `ExprLit` from the expression `"test"` and return it.
#[deprecated(since="1.0.2", note="please use `try_new_fn` instead")]
pub fn try_new_search_and_autoreplaceshielding_fn<'a, R>(a: &'a str, next: impl FnOnce(Cow<'a, ExprLit>) -> R, err: impl FnOnce(ExprLitTryNewErr) -> R) -> R {
Self::try_new_fn(
a,
|exprlit| match a.find('\\') {
None => next(Cow::Borrowed(exprlit)),
Some(pos) => {
let mut result = String::with_capacity(a.len());

let all_str = match pos {
0 => exprlit.as_str(),
pos => {
let (push_str, all_str) = exprlit.split_at(pos-1);
result.push_str(push_str);

all_str
},
};

let mut iter = all_str.chars();
while let Some(asymb) = iter.next() {
if asymb == '\\' {
match iter.next() {
Some(asymb) => result.push(asymb),
None => break,
}

continue;
}

result.push(asymb);
}

next(Cow::Owned(result))
},
},
|e| err(e)
)
}

#[inline(always)]
/// Returns `true` if self has a length of zero bytes.
pub const fn is_empty(&self) -> bool {
Expand All @@ -235,7 +187,6 @@ fn test_literal() {
/*
Checking the correct operation of ExprLit.
*/

assert_eq!(
ExprLit::try_new(""),
Err(ExprLitTryNewErr::ExpLen { current: 0, exp: 2 })
Expand Down
Loading

0 comments on commit 92cb8ff

Please sign in to comment.