Skip to content

Commit 9d690ef

Browse files
author
hg-anssi
committed
put examples in rust project
1 parent 1a5f2f6 commit 9d690ef

File tree

17 files changed

+690
-1043
lines changed

17 files changed

+690
-1043
lines changed

examples/src/counter.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <stdint.h>
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <inttypes.h>
5+
6+
#include "counter.h"
7+
8+
int main(int argc, const char **argv)
9+
{
10+
if (argc < 2)
11+
{
12+
return -1;
13+
}
14+
size_t n = (size_t)strtoull(argv[1], NULL, 10);
15+
16+
Counter *c = counter_create();
17+
for (size_t i = 0; i < n; i++)
18+
{
19+
if (counter_incr(c) != 0)
20+
{
21+
printf("overflow\n");
22+
counter_destroy(c);
23+
return -1;
24+
}
25+
}
26+
27+
printf("%" PRIu32 "\n", counter_get(c));
28+
counter_destroy(c);
29+
30+
return 0;
31+
}

examples/src/counter.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <stdarg.h>
2+
#include <stdbool.h>
3+
#include <stdint.h>
4+
#include <stdlib.h>
5+
6+
typedef struct Counter Counter;
7+
8+
Counter *counter_create(void);
9+
10+
int counter_destroy(Counter *counter);
11+
12+
uint32_t counter_get(const Counter *counter);
13+
14+
int counter_incr(Counter *counter);

examples/src/counter.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/// Opaque counter
2+
pub struct Counter(u32);
3+
4+
impl Counter {
5+
/// Create a counter (initially at 0)
6+
fn new() -> Self {
7+
Self(0)
8+
}
9+
/// Get the current value of the counter
10+
fn get(&self) -> u32 {
11+
self.0
12+
}
13+
/// Increment the value of the counter if there's no overflow
14+
fn incr(&mut self) -> bool {
15+
if let Some(n) = self.0.checked_add(1) {
16+
self.0 = n;
17+
true
18+
} else {
19+
false
20+
}
21+
}
22+
}
23+
24+
// C-compatible API
25+
26+
#[unsafe(no_mangle)]
27+
pub unsafe extern "C" fn counter_create() -> *mut Counter {
28+
Box::into_raw(Box::new(Counter::new()))
29+
}
30+
31+
#[unsafe(no_mangle)]
32+
pub unsafe extern "C" fn counter_incr(counter: *mut Counter) -> std::os::raw::c_int {
33+
if let Some(counter) = unsafe { counter.as_mut() } {
34+
if counter.incr() { 0 } else { -1 }
35+
} else {
36+
-2
37+
}
38+
}
39+
40+
#[unsafe(no_mangle)]
41+
pub unsafe extern "C" fn counter_get(counter: *const Counter) -> u32 {
42+
if let Some(counter) = unsafe { counter.as_ref() } {
43+
counter.get()
44+
} else {
45+
0
46+
}
47+
}
48+
49+
#[unsafe(no_mangle)]
50+
pub unsafe extern "C" fn counter_destroy(counter: *mut Counter) -> std::os::raw::c_int {
51+
if !counter.is_null() {
52+
let _ = unsafe { Box::from_raw(counter) }; // get box and drop
53+
0
54+
} else {
55+
-1
56+
}
57+
}

examples/src/ffi.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include <stdint.h>
2+
#include <stdio.h>
3+
4+
/////////////////////// ANCHOR: extern_struct
5+
struct Data
6+
{
7+
uint32_t a;
8+
uint16_t b;
9+
uint64_t c;
10+
};
11+
__attribute__((packed)) struct PackedData
12+
{
13+
uint32_t a;
14+
uint16_t b;
15+
uint64_t c;
16+
};
17+
/////////////////////// ANCHOR_END: extern_struct
18+
19+
/////////////////////// ANCHOR: pointers
20+
#include <stdint.h>
21+
#include <inttypes.h>
22+
23+
//! Add in place
24+
void add_in_place(uint32_t *a, uint32_t b);
25+
26+
int use_add_in_place()
27+
{
28+
uint32_t x = 25;
29+
add_in_place(&x, 17);
30+
printf("%" PRIu32 " == 42", x);
31+
return 0;
32+
}
33+
/////////////////////// ANCHOR_END: pointers
34+
35+
/////////////////////// ANCHOR: function_pointers
36+
uint32_t repeat(uint32_t start, uint32_t n, uint32_t (*f)(uint32_t));
37+
/////////////////////// ANCHOR_END: function_pointers
38+
39+
/////////////////////// ANCHOR: free_intern
40+
struct XtraResource;
41+
void xtra_with(void (*cb)(struct XtraResource *xtra));
42+
void xtra_sthg(struct XtraResource *xtra);
43+
44+
void cb(struct XtraResource *xtra)
45+
{
46+
// ()...) do anything with the proposed C API for XtraResource
47+
xtra_sthg(xtra);
48+
}
49+
50+
int use_xtra()
51+
{
52+
xtra_with(cb);
53+
}
54+
/////////////////////// ANCHOR_END: free_intern

0 commit comments

Comments
 (0)