Skip to content

Commit e1e516b

Browse files
authored
Merge pull request #2 from clucompany/1.0.7
1.0.7
2 parents 96d7602 + eebd26b commit e1e516b

File tree

15 files changed

+693
-543
lines changed

15 files changed

+693
-543
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "include_tt"
3-
version = "1.0.6"
3+
version = "1.0.7"
44
edition = "2024"
55
authors = ["Denis Kotlyarov (Денис Котляров) <denis2005991@gmail.com>"]
66
repository = "https://github.com/clucompany/include_tt.git"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Add this to your Cargo.toml:
3737

3838
```toml
3939
[dependencies]
40-
include_tt = "1.0.5"
40+
include_tt = "1.0.7"
4141
```
4242

4343
and this to your source code:

examples/constmodule.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
#[macro_export]
22
macro_rules! __test_custom_path {
3-
[ @$n: ident($const_t: ident) : [$t1:tt $t2:tt $t3:tt $t4:tt $t5:tt $t6:tt]; ] => {
3+
[ @($const_t: ident) : [$t1:tt $t2:tt $t3:tt $t4:tt $t5:tt $t6:tt]; ] => {
44
include_tt::include_tt! {
55
#[allow(dead_code)]
66
#[allow(non_upper_case_globals)]
77
pub mod ttest {
88
pub const a: usize = 0;
99
pub const b: usize = 10;
1010

11+
#POINT_TRACKER_FILES;
1112
pub const $const_t: (usize, usize) = (#include!([$t1 $t2 $t3 $t4 $t5 $t6]));
1213
}
1314
}
@@ -20,7 +21,7 @@ fn main() {
2021
// if you need to change, for example, to (b,a) or substitute constant values,
2122
// we will only change the contents of the file "for_examples/full.tt"!
2223
__test_custom_path! {
23-
@ttest(T): [examples / "full" . t 't']; // this file contains "a, b", see "for_examples/full.tt"
24+
@(T): [examples / "full" . t 't']; // this file contains "a, b", see "for_examples/full.tt"
2425
}
2526
assert_eq!(ttest::T, (0, 10));
2627
}

examples/full.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ macro_rules! test2_rules {
3434
}
3535
};
3636

37-
[$($tt:tt)+] => {
37+
[
38+
$($tt:tt)+
39+
] => {
3840
compile_error!(stringify!( $($tt)* ))
3941
};
4042
[] => []
@@ -43,9 +45,10 @@ macro_rules! test2_rules {
4345
fn main() {
4446
// Loading trees from a file and substituting them into a custom macro.
4547
include_tt! {
48+
#POINT_TRACKER_FILES;
4649
test2_rules! {
47-
[#include!("./examples/full.tt")] // this file contains `a, b`.
48-
[#include! { "./examples/full.tt"}] // this file contains `a, b`.
50+
[ #include!("./examples/full.tt") ] // this file contains `a, b`.
51+
[ #include! { "./examples/full.tt" } ] // this file contains `a, b`.
4952
}
5053
test2_rules! {
5154
#include!("./examples/full.tt") // this file contains `a, b`.

examples/mrules.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ fn main() {
4040
// a + b = n
4141
// or
4242
// a - b = n
43+
#POINT_TRACKER_FILES;
4344
test_rules! {
4445
#include!("./examples/mrules.tt") // this file contains "a + b = n", see "./for_examples/mrules.tt"
4546
}

src/exprs/literal.rs

Lines changed: 23 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
use crate::throw_sg_err;
2-
use alloc::{borrow::ToOwned, string::String};
32
use core::{
4-
borrow::Borrow,
53
fmt::{Debug, Display},
64
ops::Deref,
75
};
86
use proc_macro2::{Span, TokenStream as TokenStream2};
7+
use std::ffi::OsStr;
98

109
/// The actual literal expression, written as "./test".
1110
#[repr(transparent)]
12-
pub struct ExprLit {
13-
data: str,
14-
}
11+
pub struct ExprLit(str);
1512

1613
impl PartialEq<ExprLit> for ExprLit {
1714
#[inline]
@@ -27,6 +24,13 @@ impl PartialEq<str> for ExprLit {
2724
}
2825
}
2926

27+
impl AsRef<OsStr> for ExprLit {
28+
#[inline]
29+
fn as_ref(&self) -> &OsStr {
30+
AsRef::as_ref(self.as_str())
31+
}
32+
}
33+
3034
/// Errors received in case of a
3135
/// literal expression parsing error.
3236
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
@@ -39,6 +43,11 @@ pub enum ExprLitTryNewErr {
3943
}
4044

4145
impl ExprLitTryNewErr {
46+
#[inline]
47+
pub const fn expr_len(current: usize, exp: usize) -> Self {
48+
Self::ExpLen { current, exp }
49+
}
50+
4251
/// Convert an error to a syntax tree.
4352
#[inline]
4453
pub fn into_tt_err(self, span: Span) -> TokenStream2 {
@@ -76,22 +85,6 @@ impl Display for ExprLit {
7685
}
7786
}
7887

79-
impl ToOwned for ExprLit {
80-
type Owned = String;
81-
82-
#[inline]
83-
fn to_owned(&self) -> Self::Owned {
84-
self.as_str().to_owned()
85-
}
86-
}
87-
88-
impl Borrow<ExprLit> for String {
89-
#[inline]
90-
fn borrow(&self) -> &ExprLit {
91-
unsafe { ExprLit::unchecked(self.as_str()) } // TODO
92-
}
93-
}
94-
9588
impl ExprLit {
9689
/// Creating `ExprLit` without clipping.
9790
#[inline]
@@ -103,7 +96,7 @@ impl ExprLit {
10396

10497
/// Creating `ExprLit` without clipping.
10598
#[inline]
106-
pub const unsafe fn unchecked(a: &str) -> &Self {
99+
pub const unsafe fn new_unchecked(a: &str) -> &Self {
107100
Self::__new(a)
108101
}
109102

@@ -124,10 +117,7 @@ impl ExprLit {
124117

125118
let len = a_array.len();
126119
if len < 2 {
127-
return err(ExprLitTryNewErr::ExpLen {
128-
current: len,
129-
exp: 2,
130-
});
120+
return err(ExprLitTryNewErr::expr_len(len, 2));
131121
}
132122
debug_assert!({
133123
#[allow(clippy::get_first)]
@@ -142,6 +132,7 @@ impl ExprLit {
142132
/*
143133
This is safe, the extra necessary checks are done in a separate `if` above.
144134
*/
135+
145136
match unsafe { (a_array.get_unchecked(0), a_array.get_unchecked(len - 1)) } {
146137
(b'"', b'"') =>
147138
/* line */
@@ -152,10 +143,7 @@ impl ExprLit {
152143
We exclude the possibility of using `'` as more
153144
than one character.
154145
*/
155-
return err(ExprLitTryNewErr::ExpLen {
156-
current: len,
157-
exp: 3,
158-
});
146+
return err(ExprLitTryNewErr::expr_len(len, 3));
159147
}
160148
(b'\'', b'\'') =>
161149
/* line */
@@ -172,16 +160,17 @@ impl ExprLit {
172160
})
173161
}
174162

163+
#[allow(dead_code)]
175164
#[inline]
176165
/// Returns `true` if self has a length of zero bytes.
177166
pub const fn is_empty(&self) -> bool {
178-
self.data.is_empty()
167+
self.0.is_empty()
179168
}
180169

181170
/// Getting a string of actual data.
182171
#[inline]
183172
pub const fn as_str(&self) -> &str {
184-
&self.data
173+
&self.0
185174
}
186175
}
187176

@@ -191,13 +180,10 @@ fn test_literal() {
191180
/*
192181
Checking the correct operation of ExprLit.
193182
*/
194-
assert_eq!(
195-
ExprLit::try_new(""),
196-
Err(ExprLitTryNewErr::ExpLen { current: 0, exp: 2 })
197-
);
183+
assert_eq!(ExprLit::try_new(""), Err(ExprLitTryNewErr::expr_len(0, 2)));
198184
assert_eq!(
199185
ExprLit::try_new("\""),
200-
Err(ExprLitTryNewErr::ExpLen { current: 1, exp: 2 })
186+
Err(ExprLitTryNewErr::expr_len(1, 2))
201187
);
202188
assert_eq!(ExprLit::try_new("\"\""), Ok(ExprLit::__new("")),);
203189
assert_eq!(ExprLit::try_new("'\\'"), Ok(ExprLit::__new("\\")),);

0 commit comments

Comments
 (0)