New Procedural Macros Crate v0.1

This commit is contained in:
J Henry Waugh
2020-08-01 11:52:26 -05:00
parent 99d56b8f81
commit d01203cb5d
28 changed files with 2230 additions and 5 deletions

View File

@@ -0,0 +1,22 @@
use rhai::export_fn;
struct NonClonable {
a: f32,
b: u32,
c: char,
d: bool,
}
#[export_fn]
pub fn test_fn(input: &NonClonable) -> bool {
input.d
}
fn main() {
let n = NonClonable { a: 0.0, b: 10, c: 'a', d: true };
if test_fn(n) {
println!("yes");
} else {
println!("no");
}
}

View File

@@ -0,0 +1,11 @@
error: references from Rhai in this position must be mutable
--> $DIR/first_shared_ref.rs:11:23
|
11 | pub fn test_fn(input: &NonClonable) -> bool {
| ^^^^^^^^^^^^
error[E0425]: cannot find function `test_fn` in this scope
--> $DIR/first_shared_ref.rs:17:8
|
17 | if test_fn(n) {
| ^^^^^^^ not found in this scope

View File

@@ -0,0 +1,22 @@
use rhai::export_fn;
struct NonClonable {
a: f32,
b: u32,
c: char,
d: bool,
}
#[export_fn]
pub fn test_fn(input: NonClonable) -> bool {
input.d
}
fn main() {
let n = NonClonable { a: 0.0, b: 10, c: 'a', d: true };
if test_fn(n) {
println!("yes");
} else {
println!("no");
}
}

View File

@@ -0,0 +1,5 @@
error[E0277]: the trait bound `NonClonable: std::clone::Clone` is not satisfied
--> $DIR/non_clonable.rs:11:23
|
11 | pub fn test_fn(input: NonClonable) -> bool {
| ^^^^^^^^^^^ the trait `std::clone::Clone` is not implemented for `NonClonable`

View File

@@ -0,0 +1,22 @@
use rhai::export_fn;
struct NonClonable {
a: f32,
b: u32,
c: char,
d: bool,
}
#[export_fn]
pub fn test_fn(a: u32, b: NonClonable) -> bool {
a == 0 && b.d
}
fn main() {
let n = NonClonable { a: 0.0, b: 10, c: 'a', d: true };
if test_fn(10, n) {
println!("yes");
} else {
println!("no");
}
}

View File

@@ -0,0 +1,5 @@
error[E0277]: the trait bound `NonClonable: std::clone::Clone` is not satisfied
--> $DIR/non_clonable_second.rs:11:27
|
11 | pub fn test_fn(a: u32, b: NonClonable) -> bool {
| ^^^^^^^^^^^ the trait `std::clone::Clone` is not implemented for `NonClonable`

View File

@@ -0,0 +1,23 @@
use rhai::export_fn;
#[derive(Clone)]
struct Clonable {
a: f32,
b: u32,
c: char,
d: bool,
}
#[export_fn]
pub fn test_fn(input: &mut Clonable) -> &mut bool {
&mut input.d
}
fn main() {
let n = Clonable { a: 0.0, b: 10, c: 'a', d: true };
if test_fn(n) {
println!("yes");
} else {
println!("no");
}
}

View File

@@ -0,0 +1,11 @@
error: cannot return a reference to Rhai
--> $DIR/return_mut_ref.rs:12:38
|
12 | pub fn test_fn(input: &mut Clonable) -> &mut bool {
| ^^^^^^^^^^^^
error[E0425]: cannot find function `test_fn` in this scope
--> $DIR/return_mut_ref.rs:18:8
|
18 | if test_fn(n) {
| ^^^^^^^ not found in this scope

View File

@@ -0,0 +1,19 @@
use rhai::export_fn;
#[derive(Clone)]
struct Clonable {
a: f32,
b: u32,
c: char,
d: bool,
}
#[export_fn]
pub fn test_fn(input: Clonable) -> *const str {
"yes"
}
fn main() {
let n = Clonable { a: 0.0, b: 10, c: 'a', d: true };
println!("{}", unsafe { let ptr = test_fn(n); *ptr });
}

View File

@@ -0,0 +1,11 @@
error: cannot return a pointer to Rhai
--> $DIR/return_pointer.rs:12:33
|
12 | pub fn test_fn(input: Clonable) -> *const str {
| ^^^^^^^^^^^^^
error[E0425]: cannot find function `test_fn` in this scope
--> $DIR/return_pointer.rs:18:39
|
18 | println!("{}", unsafe { let ptr = test_fn(n); *ptr });
| ^^^^^^^ not found in this scope

View File

@@ -0,0 +1,19 @@
use rhai::export_fn;
#[derive(Clone)]
struct Clonable {
a: f32,
b: u32,
c: char,
d: bool,
}
#[export_fn]
pub fn test_fn(input: Clonable) -> & 'static str {
"yes"
}
fn main() {
let n = Clonable { a: 0.0, b: 10, c: 'a', d: true };
println!("{}", test_fn(n));
}

View File

@@ -0,0 +1,11 @@
error: cannot return a reference to Rhai
--> $DIR/return_shared_ref.rs:12:33
|
12 | pub fn test_fn(input: Clonable) -> & 'static str {
| ^^^^^^^^^^^^^^^^
error[E0425]: cannot find function `test_fn` in this scope
--> $DIR/return_shared_ref.rs:18:20
|
18 | println!("{}", test_fn(n));
| ^^^^^^^ not found in this scope

View File

@@ -0,0 +1,23 @@
use rhai::export_fn;
#[derive(Clone)]
pub struct Clonable {
a: f32,
b: u32,
c: char,
d: bool,
}
#[export_fn]
pub fn test_fn(input: Clonable, factor: &bool) -> bool {
input.d & factor
}
fn main() {
let n = Clonable { a: 0.0, b: 10, c: 'a', d: true };
if test_fn(n, &true) {
println!("yes");
} else {
println!("no");
}
}

View File

@@ -0,0 +1,11 @@
error: this type in this position passes from Rhai by value
--> $DIR/second_shared_ref.rs:12:41
|
12 | pub fn test_fn(input: Clonable, factor: &bool) -> bool {
| ^^^^^
error[E0425]: cannot find function `test_fn` in this scope
--> $DIR/second_shared_ref.rs:18:8
|
18 | if test_fn(n, &true) {
| ^^^^^^^ not found in this scope