Skip to content

Commit bb9f701

Browse files
committed
Add c/c++ call rust lib example
1 parent 8875fa9 commit bb9f701

File tree

8 files changed

+81
-23
lines changed

8 files changed

+81
-23
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ edition = "2018"
77
[workspace]
88
members = [
99
"rust_call_cpp_static_lib",
10+
"c_call_rust_lib"
1011
]
1112

1213
[dev-dependencies]

c_call_rust_lib/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "c_call_rust_lib"
3+
version = "0.1.0"
4+
authors = ["wuaoxiang <[email protected]>"]
5+
edition = "2018"
6+
7+
[lib]
8+
name = "c_call_rust_lib"
9+
crate-type = ["staticlib", "cdylib"]
10+
11+
[dependencies]

c_call_rust_lib/a.out

1.81 MB
Binary file not shown.

c_call_rust_lib/main.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
void p();
2+
void hello(const char *str);
3+
4+
/*
5+
static link: gcc main.c ../target/debug/libc_call_rust_lib.a
6+
dynamic link: gcc main.c -Isrc -L ../target/debug/ -lc_call_rust_lib
7+
*/
8+
int main() {
9+
p();
10+
hello("rust");
11+
return 0;
12+
}

c_call_rust_lib/src/lib.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use std::os::raw::{c_char, c_int};
2+
use std::ffi::CStr;
3+
4+
#[no_mangle]
5+
pub extern "C" fn p() {
6+
println!("call println in rust code");
7+
}
8+
9+
#[no_mangle]
10+
pub extern "C" fn hello(name: *const c_char) {
11+
let c_str = unsafe {
12+
CStr::from_ptr(name)
13+
};
14+
println!("hello {:?}", c_str);
15+
}
16+
17+
#[no_mangle]
18+
pub extern "C" fn repeat_hi()

examples/const_fn.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const fn gcd(x: u32, y: u32) -> u32 {
2-
let (mut a, mut b) = if x > y {(x, y)} else {(y, x)};
2+
let (mut a, mut b) = if x > y { (x, y) } else { (y, x) };
33
let mut temp = a % b;
44
while temp != 0 {
55
a = b;
@@ -13,11 +13,7 @@ const GCD: u32 = gcd(18, 12);
1313

1414
const fn fib(n: u32) -> u32 {
1515
const fn helper(n: u32, a: u32, b: u32) -> u32 {
16-
return if n <= 2 {
17-
b
18-
} else {
19-
helper(n - 1, b, a + b)
20-
}
16+
return if n <= 2 { b } else { helper(n - 1, b, a + b) };
2117
}
2218
helper(n, 1, 1)
2319
}
@@ -28,4 +24,4 @@ const FIB_5: u32 = fib(5);
2824
fn main() {
2925
dbg!(GCD);
3026
dbg!(FIB_5);
31-
}
27+
}

examples/const_generic.rs

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#![feature(const_generics)]
2-
1+
#![feature(const_generics, const_fn, const_in_array_repeat_expressions)]
32
use std::mem::MaybeUninit;
43

54
/**
@@ -8,7 +7,18 @@ use std::mem::MaybeUninit;
87
*/
98
struct Array<T, const N: usize> {
109
items: [MaybeUninit<T>; N],
11-
length: usize
10+
length: usize,
11+
}
12+
13+
trait ArrayLen {
14+
fn len(&self) -> usize;
15+
}
16+
17+
impl<T, const N: usize> ArrayLen for Array<T, { N }> {
18+
#[inline]
19+
fn len(&self) -> usize {
20+
self.length
21+
}
1222
}
1323

1424
/**
@@ -17,20 +27,21 @@ struct Array<T, const N: usize> {
1727
> Syntactically we may distinguish these expressions with braces
1828
两个常量表达式的typechecking是T-lang团队的主要问题,目前{N+1}和{1+N}两个常量泛型长度会不一样
1929
*/
20-
impl<T, const N: usize> Array<T, {N}> {
21-
fn new() -> Self {
22-
// Self {
23-
// items: [MaybeUninit<T>; N],
24-
// length: N
25-
// }
26-
todo!("")
30+
impl<T: Copy, const N: usize> Array<T, { N }> {
31+
const fn new() -> Self {
32+
Self {
33+
items: [MaybeUninit::uninit(); N],
34+
length: 0,
35+
}
36+
}
37+
38+
#[inline]
39+
const fn capacity(&self) -> usize {
40+
N
2741
}
2842
}
2943

3044
fn main() {
31-
// fn foo() -> impl ToString {
32-
// "Hello, world!"
33-
// }
34-
// let _: &str = foo();
35-
// let a: Array<bool, 1> = Array::new();
36-
}
45+
let visited: Array<bool, 8> = Array::new();
46+
dbg!(visited.len(), visited.capacity());
47+
}

examples/itertools_batching_group_array.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1+
#![feature(array_chunks)]
12
use itertools::Itertools;
23

4+
#[test]
5+
fn generic_const_api_array_chunks() {
6+
let data = [1, 2, 3, 4, 5, 6];
7+
// 分割数组,每组计算完之后再求和
8+
let sum = data.array_chunks().map(|&[a1, a2]| a1 * a2).sum::<i32>();
9+
assert_eq!(sum, (1 * 2) + (3 * 4) + (5 * 6));
10+
}
11+
312
fn main() {
413
let data = ["1", "22", "4444", "666666", "999999999"];
514
let batch_size = 10;

0 commit comments

Comments
 (0)