@@ -19,52 +19,47 @@ You can find more information about using Rust from other languages in
1919
2020## Examples
2121
22+ Creating FFIs to use a Rust's ` struct ` methods from C or C++:
23+
2224``` rust
23- struct TestIt {
24- value : u8 ,
25- }
25+ struct TestIt { value : u8 }
2626
2727impl TestIt {
28- pub fn new (value : u8 ) -> Self {
29- Self {
30- value ,
31- }
32- }
33- pub fn add (& mut self , value : u8 ) {
34- self . value += value ;
35- }
36- pub fn get (& self ) -> u8 {
37- self . value
38- }
28+ pub fn add (& mut self , value : u8 ) { self . value += value }
29+ pub fn get (& self ) -> u8 { self . value }
3930}
4031
41- /// TestIt new method .
32+ /// Ownership will NOT control the heap-allocated memory until own it back .
4233#[no_mangle]
43- pub extern fn testit_new (value : u8 ) -> * mut TestIt {
44- opaque_pointer :: raw (TestIt :: new ( value ))
34+ pub extern fn test_it_new (value : u8 ) -> * mut TestIt {
35+ return opaque_pointer :: raw (TestIt { value });
4536}
4637
47- /// TestIt add method .
38+ /// Drop (free memory of) Rust's TestIt object as usually .
4839#[no_mangle]
49- pub extern fn testit_add ( testit : * mut TestIt , value : u8 ) {
50- let testit = unsafe { opaque_pointer :: mut_object ( testit ) };
51- testit . add ( value );
40+ pub extern fn test_it_free ( test_it : * mut TestIt ) {
41+ let test_it = unsafe { opaque_pointer :: own_back ( test_it ) };
42+ // You can use it or only let it be dropped.
5243}
5344
54- /// TestIt get method.
5545#[no_mangle]
56- pub extern fn testit_get (testit : * const TestIt ) -> u8 {
57- let testit = unsafe { opaque_pointer :: object (testit ) };
58- testit . get ()
46+ pub extern fn test_it_add (test_it : * mut TestIt , value : u8 ) {
47+ let test_it = unsafe { opaque_pointer :: mut_object (test_it ) };
48+ test_it . add (value );
49+ // Here will NOT be dropped, the pointer continues been valid.
5950}
6051
61- /// TestIt free.
6252#[no_mangle]
63- pub extern fn testit_free (testit : * mut TestIt ) {
64- unsafe { opaque_pointer :: free (testit ) }
53+ pub extern fn test_it_get (test_it : * const TestIt ) -> u8 {
54+ let test_it = unsafe { opaque_pointer :: object (test_it ) };
55+ return test_it . get ();
56+ // Here will NOT be dropped, the pointer continues been valid.
6557}
6658```
6759
60+ The previous example is compiled when tests are run. If you have an error
61+ with that code, please, [ open a issue] ( https://github.com/jhg/opaque-pointer-rs/issues?q=is%3Aissue+is%3Aopen ) .
62+
6863## Features
6964
7065- ` std ` : activated by default, it is required for functions using std
0 commit comments