Skip to content

Commit 8f1672c

Browse files
committed
1. Added example "easy_use"
2. Improved example "easy" 3. IMproved docs
1 parent cb8534e commit 8f1672c

File tree

6 files changed

+120
-81
lines changed

6 files changed

+120
-81
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "clucstr"
3-
version = "1.1.8"
4-
authors = ["Денис Котляров <#Ulin Project 18, denis2005991@gmail.com>"]
3+
version = "1.1.9"
4+
authors = ["Denis Kotlyarov (Денис Котляров) <denis2005991@gmail.com>"]
55
repository = "https://github.com/clucompany/cluCStr.git"
66
edition = "2018"
77

README.md

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[![crates.io](http://meritbadge.herokuapp.com/clucstr)](https://crates.io/crates/clucstr)
55
[![Documentation](https://docs.rs/clucstr/badge.svg)](https://docs.rs/clucstr)
66

7-
Safe creation of 'CStr' with zero cost at a compilation stage with check of zero bytes and a possibility of communication of several values.
7+
Safe creation of CStr with zero cost at the compilation stage with checking for zero bytes and the ability to transfer multiple values.
88

99
# Features
1010
1. Creation of safe CStr at a compilation stage.
@@ -14,50 +14,56 @@ Safe creation of 'CStr' with zero cost at a compilation stage with check of zero
1414

1515
# Use
1616
```
17+
1718
#![feature(plugin)]
1819
#![plugin(clucstr)]
1920
2021
use std::ffi::CStr;
2122
2223
fn main() {
23-
let c_str = cstr!("cluWorld");
24-
//"cluWorld" <-- [99, 108, 117, 87, 111, 114, 108, 100, 0], len:9
24+
let cstr_1 = cstr!("cluWorld");
25+
assert_eq!(cstr_1.to_bytes_with_nul(), b"cluWorld\0");
26+
//"cluWorld", [99, 108, 117, 87, 111, 114, 108, 100, 0], len:9
2527
26-
let c_str2 = cstr!("cluWorld\0");
27-
//"cluWorld" <-- [99, 108, 117, 87, 111, 114, 108, 100, 0], len:9
28+
let cstr_2 = cstr!("cluWorld\0");
29+
//"cluWorld", [99, 108, 117, 87, 111, 114, 108, 100, 0], len:9
30+
assert_eq!(cstr_2.to_bytes_with_nul(), b"cluWorld\0");
2831
29-
let c_str3 = cstr!("clu", b"World");
30-
//"cluWorld" <-- [99, 108, 117, 87, 111, 114, 108, 100, 0], len:9
32+
let cstr_3 = cstr!("clu", b"World");
33+
//"cluWorld", [99, 108, 117, 87, 111, 114, 108, 100, 0], len:9
34+
assert_eq!(cstr_3.to_bytes_with_nul(), b"cluWorld\0");
3135
32-
let c_str4 = cstr!(
36+
let cstr_4 = cstr!(
3337
b'c', b'l', b'u',
3438
b'W', b'o', b'r', b'l', b'd',
3539
0
3640
);
37-
//"cluWorld" <-- [99, 108, 117, 87, 111, 114, 108, 100, 0], len:9
41+
//"cluWorld", [99, 108, 117, 87, 111, 114, 108, 100, 0], len:9
42+
assert_eq!(cstr_4.to_bytes_with_nul(), b"cluWorld\0");
3843
39-
let c_str5 = cstr!(
44+
let cstr_5 = cstr!(
4045
"clu",
4146
//It is possible to insert such values as: [u8], & 'static str, u8, i8, (0 without specifying the type).
4247
4348
b'W', b'o', b'r', b'l', b"d\0"
4449
//The zero byte is automatically added, it is possible to write it, and it is possible not to write.
4550
//It is forbidden to insert zero byte in the middle or at the beginning of a line.
4651
);
47-
//"cluWorld" <-- [99, 108, 117, 87, 111, 114, 108, 100, 0], len:9
52+
//"cluWorld", [99, 108, 117, 87, 111, 114, 108, 100, 0], len:9
53+
assert_eq!(cstr_5.to_bytes_with_nul(), b"cluWorld\0");
4854
49-
my_function(c_str);
50-
my_function(c_str2);
51-
my_function(c_str3);
52-
my_function(c_str4);
53-
my_function(c_str5);
55+
my_function(1, cstr_1);
56+
my_function(2, cstr_2);
57+
my_function(3, cstr_3);
58+
my_function(4, cstr_4);
59+
my_function(5, cstr_5);
5460
}
5561
56-
fn my_function(a: &'static CStr) {
62+
fn my_function(num: usize, a: &'static CStr) {
5763
//'static --> it is possible not to write.
5864
5965
let c_arr = a.to_bytes_with_nul();
60-
println!("{:?} <-- array: {:?}, len: {}", a, c_arr, c_arr.len());
66+
println!("#cstr_{} {:?}, array: {:?}, len: {}", num, a, c_arr, c_arr.len());
6167
}
6268
```
6369

examples/easy_use.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
#![feature(plugin)]
3+
#![plugin(clucstr)]
4+
5+
use std::ffi::CStr;
6+
7+
fn main() {
8+
let cstr = cstr!("My CSTR!");
9+
//&CStr
10+
11+
assert_eq!(cstr.to_bytes(), b"My CSTR!");
12+
assert_eq!(cstr.to_bytes_with_nul(), b"My CSTR!\0");
13+
}
14+

examples/use.rs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,47 @@
55
use std::ffi::CStr;
66

77
fn main() {
8-
let c_str = cstr!("cluWorld");
9-
//"cluWorld" <-- [99, 108, 117, 87, 111, 114, 108, 100, 0], len:9
8+
let cstr_1 = cstr!("cluWorld");
9+
assert_eq!(cstr_1.to_bytes_with_nul(), b"cluWorld\0");
10+
//"cluWorld", [99, 108, 117, 87, 111, 114, 108, 100, 0], len:9
1011

11-
let c_str2 = cstr!("cluWorld\0");
12-
//"cluWorld" <-- [99, 108, 117, 87, 111, 114, 108, 100, 0], len:9
12+
let cstr_2 = cstr!("cluWorld\0");
13+
//"cluWorld", [99, 108, 117, 87, 111, 114, 108, 100, 0], len:9
14+
assert_eq!(cstr_2.to_bytes_with_nul(), b"cluWorld\0");
1315

14-
let c_str3 = cstr!("clu", b"World");
15-
//"cluWorld" <-- [99, 108, 117, 87, 111, 114, 108, 100, 0], len:9
16+
let cstr_3 = cstr!("clu", b"World");
17+
//"cluWorld", [99, 108, 117, 87, 111, 114, 108, 100, 0], len:9
18+
assert_eq!(cstr_3.to_bytes_with_nul(), b"cluWorld\0");
1619

17-
let c_str4 = cstr!(
20+
let cstr_4 = cstr!(
1821
b'c', b'l', b'u',
1922
b'W', b'o', b'r', b'l', b'd',
2023
0
2124
);
22-
//"cluWorld" <-- [99, 108, 117, 87, 111, 114, 108, 100, 0], len:9
25+
//"cluWorld", [99, 108, 117, 87, 111, 114, 108, 100, 0], len:9
26+
assert_eq!(cstr_4.to_bytes_with_nul(), b"cluWorld\0");
2327

24-
let c_str5 = cstr!(
28+
let cstr_5 = cstr!(
2529
"clu",
2630
//It is possible to insert such values as: [u8], & 'static str, u8, i8, (0 without specifying the type).
2731

2832
b'W', b'o', b'r', b'l', b"d\0"
2933
//The zero byte is automatically added, it is possible to write it, and it is possible not to write.
3034
//It is forbidden to insert zero byte in the middle or at the beginning of a line.
3135
);
32-
//"cluWorld" <-- [99, 108, 117, 87, 111, 114, 108, 100, 0], len:9
36+
//"cluWorld", [99, 108, 117, 87, 111, 114, 108, 100, 0], len:9
37+
assert_eq!(cstr_5.to_bytes_with_nul(), b"cluWorld\0");
3338

34-
my_function(c_str);
35-
my_function(c_str2);
36-
my_function(c_str3);
37-
my_function(c_str4);
38-
my_function(c_str5);
39+
my_function(1, cstr_1);
40+
my_function(2, cstr_2);
41+
my_function(3, cstr_3);
42+
my_function(4, cstr_4);
43+
my_function(5, cstr_5);
3944
}
4045

41-
fn my_function(a: &'static CStr) {
46+
fn my_function(num: usize, a: &'static CStr) {
4247
//'static --> it is possible not to write.
4348

4449
let c_arr = a.to_bytes_with_nul();
45-
println!("{:?} <-- array: {:?}, len: {}", a, c_arr, c_arr.len());
50+
println!("#cstr_{} {:?}, array: {:?}, len: {}", num, a, c_arr, c_arr.len());
4651
}

src/lib.rs

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
//
1818

1919
/*!
20-
Safe creation of 'CStr' with zero cost at a compilation stage with check of zero bytes and a possibility of communication of several values.
20+
Safe creation of CStr with zero cost at the compilation stage with checking for zero bytes and the ability to transfer multiple values.
2121
2222
# Features
2323
1. Creation of safe CStr at a compilation stage.
@@ -27,50 +27,56 @@ Safe creation of 'CStr' with zero cost at a compilation stage with check of zero
2727
2828
# Use
2929
```
30+
3031
#![feature(plugin)]
3132
#![plugin(clucstr)]
3233
3334
use std::ffi::CStr;
3435
3536
fn main() {
36-
let c_str = cstr!("cluWorld");
37-
//"cluWorld" <-- [99, 108, 117, 87, 111, 114, 108, 100, 0], len:9
37+
let cstr_1 = cstr!("cluWorld");
38+
assert_eq!(cstr_1.to_bytes_with_nul(), b"cluWorld\0");
39+
//"cluWorld", [99, 108, 117, 87, 111, 114, 108, 100, 0], len:9
3840
39-
let c_str2 = cstr!("cluWorld\0");
40-
//"cluWorld" <-- [99, 108, 117, 87, 111, 114, 108, 100, 0], len:9
41+
let cstr_2 = cstr!("cluWorld\0");
42+
//"cluWorld", [99, 108, 117, 87, 111, 114, 108, 100, 0], len:9
43+
assert_eq!(cstr_2.to_bytes_with_nul(), b"cluWorld\0");
4144
42-
let c_str3 = cstr!("clu", b"World");
43-
//"cluWorld" <-- [99, 108, 117, 87, 111, 114, 108, 100, 0], len:9
45+
let cstr_3 = cstr!("clu", b"World");
46+
//"cluWorld", [99, 108, 117, 87, 111, 114, 108, 100, 0], len:9
47+
assert_eq!(cstr_3.to_bytes_with_nul(), b"cluWorld\0");
4448
45-
let c_str4 = cstr!(
49+
let cstr_4 = cstr!(
4650
b'c', b'l', b'u',
4751
b'W', b'o', b'r', b'l', b'd',
4852
0
4953
);
50-
//"cluWorld" <-- [99, 108, 117, 87, 111, 114, 108, 100, 0], len:9
54+
//"cluWorld", [99, 108, 117, 87, 111, 114, 108, 100, 0], len:9
55+
assert_eq!(cstr_4.to_bytes_with_nul(), b"cluWorld\0");
5156
52-
let c_str5 = cstr!(
57+
let cstr_5 = cstr!(
5358
"clu",
5459
//It is possible to insert such values as: [u8], & 'static str, u8, i8, (0 without specifying the type).
5560
5661
b'W', b'o', b'r', b'l', b"d\0"
5762
//The zero byte is automatically added, it is possible to write it, and it is possible not to write.
5863
//It is forbidden to insert zero byte in the middle or at the beginning of a line.
5964
);
60-
//"cluWorld" <-- [99, 108, 117, 87, 111, 114, 108, 100, 0], len:9
65+
//"cluWorld", [99, 108, 117, 87, 111, 114, 108, 100, 0], len:9
66+
assert_eq!(cstr_5.to_bytes_with_nul(), b"cluWorld\0");
6167
62-
my_function(c_str);
63-
my_function(c_str2);
64-
my_function(c_str3);
65-
my_function(c_str4);
66-
my_function(c_str5);
68+
my_function(1, cstr_1);
69+
my_function(2, cstr_2);
70+
my_function(3, cstr_3);
71+
my_function(4, cstr_4);
72+
my_function(5, cstr_5);
6773
}
6874
69-
fn my_function(a: &'static CStr) {
75+
fn my_function(num: usize, a: &'static CStr) {
7076
//'static --> it is possible not to write.
7177
7278
let c_arr = a.to_bytes_with_nul();
73-
println!("{:?} <-- array: {:?}, len: {}", a, c_arr, c_arr.len());
79+
println!("#cstr_{} {:?}, array: {:?}, len: {}", num, a, c_arr, c_arr.len());
7480
}
7581
```
7682
@@ -182,7 +188,7 @@ extern crate rustc_plugin;
182188
mod nightly;
183189
pub use self::nightly::*;
184190

185-
///Safe creation of 'CStr' at a compilation stage with check on zero bytes and a possibility of concatenation of several values.
191+
///Safe creation of CStr” with zero cost at the compilation stage with checking for zero bytes and the ability to transfer multiple values.
186192
187193
//The description only for documentation not to use.
188194
//Connect a plug-in! (The plug-in is described in nightly.rs, connect a plug-in as it is specified in documentation)

src/nightly.rs

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ pub fn cstr(cx: &mut ExtCtxt, sp: Span, args: &[TokenTree]) -> Box<dyn MacResult
6767
match unk.node {
6868
ExprKind::Lit(ref l) => {
6969
match l.node {
70+
71+
//"str"
7072
LitKind::Str(ref array, _) => {
7173
let s_array = array.as_str();
7274
let array = s_array.as_bytes();
@@ -86,6 +88,9 @@ pub fn cstr(cx: &mut ExtCtxt, sp: Span, args: &[TokenTree]) -> Box<dyn MacResult
8688
}
8789
}
8890
},
91+
//
92+
93+
//b"str"
8994
LitKind::ByteStr(ref array) => {
9095
let array = array.as_slice();
9196

@@ -104,17 +109,22 @@ pub fn cstr(cx: &mut ExtCtxt, sp: Span, args: &[TokenTree]) -> Box<dyn MacResult
104109
}
105110
}
106111
},
112+
//
113+
114+
115+
116+
107117

118+
//Int 0
119+
LitKind::Int(0, LitIntType::Unsuffixed) if unk_index+1 == args_len => break 'looper,
108120
LitKind::Int(0, LitIntType::Unsuffixed) => {
109-
if unk_index+1 == args_len {
110-
break 'looper;
111-
}else {
112-
cx.span_err(l.span, "trailing byte detected");
113-
return DummyResult::any(sp);
114-
}
121+
cx.span_err(l.span, "trailing byte detected");
122+
return DummyResult::any(sp);
115123
},
124+
//
116125

117-
LitKind::Int(ref a, LitIntType::Unsigned(UintTy::U8))
126+
//Int, i8, u8
127+
LitKind::Int(ref a, LitIntType::Unsigned(UintTy::U8))
118128
| LitKind::Int(ref a, LitIntType::Signed(IntTy::I8))
119129
=> {
120130
match a {
@@ -131,21 +141,21 @@ pub fn cstr(cx: &mut ExtCtxt, sp: Span, args: &[TokenTree]) -> Box<dyn MacResult
131141
},
132142
}
133143
},
144+
//
134145

135-
LitKind::Byte(ref a) => {
136-
match a {
137-
0u8 if unk_index+1 == args_len => break 'looper,
138-
0u8 => {
139-
cx.span_err(l.span, "trailing byte detected");
140-
return DummyResult::any(sp);
141-
},
142-
_ => {
143-
r_array.push(
144-
cx.expr_lit(sp, LitKind::Int(*a as u128, LitIntType::Unsigned(UintTy::U8)))
145-
);
146-
},
147-
}
146+
//Byte 0:
147+
LitKind::Byte(0) if unk_index+1 == args_len => break 'looper,
148+
LitKind::Byte(0) => {
149+
cx.span_err(l.span, "trailing byte detected");
150+
return DummyResult::any(sp);
148151
},
152+
//
153+
154+
//Byte
155+
LitKind::Byte(a) => r_array.push(cx.expr_lit(sp, LitKind::Int(a as u128, LitIntType::Unsigned(UintTy::U8)))),
156+
//
157+
158+
//Unk type
149159
_ => {
150160
cx.span_err(l.span, "incorrect data, was expected: &[u8], str, u8, i8");
151161
return DummyResult::any(sp);
@@ -214,13 +224,11 @@ pub fn cstr(cx: &mut ExtCtxt, sp: Span, args: &[TokenTree]) -> Box<dyn MacResult
214224
},
215225
id: ast::DUMMY_NODE_ID,
216226
rules: BlockCheckMode::Unsafe(UnsafeSource::CompilerGenerated), //<-- UNSAFE
217-
span: sp,
218-
219-
//recovered: false,
220-
//FIX!, UPDATE RUST:((
227+
span: sp,
221228
});
222229
cx.expr_block(block) //RESULT EXPR
223-
})// unsafe { &*([u8] as *const [u8] as *const CStr) }
230+
})
231+
// unsafe { &*([u8] as *const [u8] as *const CStr) }
224232
}
225233

226234

0 commit comments

Comments
 (0)