-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathffi-omnibus.raku
93 lines (69 loc) · 2.69 KB
/
ffi-omnibus.raku
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use NativeCall;
constant $n-path = './ffi-omnibus/target/debug/foo';
## Rust FFI Omnibus: Integers
## http://jakegoulding.com/rust-ffi-omnibus/integers/
sub addition(int32, int32) returns int32 is native($n-path) { * }
say addition(1, 2);
## Rust FFI Omnibus: String Arguments
## http://jakegoulding.com/rust-ffi-omnibus/string-arguments/
sub how_many_characters(Str is encoded('utf8')) returns int32 is native($n-path) { * }
say how_many_characters("göes to élevên");
## Rust FFI Omnibus: String Return Values
## http://jakegoulding.com/rust-ffi-omnibus/string-return-values/
sub theme_song_generate(uint8) returns Pointer[Str] is encoded('utf8') is native($n-path) { * }
sub theme_song_free(Pointer[Str]) is native($n-path) { * }
my \song = theme_song_generate(5);
say song.deref;
theme_song_free(song);
## Rust FFI Omnibus: Slice Arguments
## http://jakegoulding.com/rust-ffi-omnibus/slice-arguments/
sub sum_of_even(CArray[uint32], size_t) returns uint32 is native($n-path) { * }
my @numbers := CArray[uint32].new;
@numbers[$++] = $_ for 1..6;
say sum_of_even( @numbers, @numbers.elems );
## Rust FFI Omnibus: Tuples
## http:##jakegoulding.com/rust-ffi-omnibus/tuples/
#`[ getting funky values
class Tuple is repr('CStruct') {
has uint32 $.x;
has uint32 $.y;
}
sub flip_things_around(Tuple) returns Tuple is native($n-path) { * }
my \initial = Tuple.new( x => 10, y => 20 );
my \result = flip_things_around(initial);
dd result;
say result.x, result.y;
#]
## Rust FFI Omnibus: Objects
## http:##jakegoulding.com/rust-ffi-omnibus/objects/
class ZipCodeDatabase is repr('CPointer') {
sub zip_code_database_new() returns ZipCodeDatabase is native($n-path) { * }
sub zip_code_database_free(ZipCodeDatabase) is native($n-path) { * }
sub zip_code_database_populate(ZipCodeDatabase) is native($n-path) { * }
sub zip_code_database_population_of(ZipCodeDatabase, Str is encoded('utf8'))
returns uint32 is native($n-path) { * }
method new {
zip_code_database_new
}
submethod DESTROY { # Free data when the object is garbage collected.
zip_code_database_free(self);
}
method populate {
zip_code_database_populate(self)
}
method population_of( Str \zip ) {
zip_code_database_population_of(self, zip);
}
}
my \database = ZipCodeDatabase.new;
database.populate;
my \pop1 = database.population_of('90210');
my \pop2 = database.population_of('20500');
say pop1 - pop2;
# CPAN Platypus
# https://metacpan.org/pod/FFI::Platypus::Lang::Rust
sub get_lines(&callback (Str --> int32)) is native($n-path) { * }
my &line-out = sub ( $line ) {
say "line> $line";
}
get_lines( &line-out );