First commit
This commit is contained in:
		
						commit
						26d11cdba9
					
				
							
								
								
									
										4
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,4 @@
 | 
				
			|||||||
 | 
					/target
 | 
				
			||||||
 | 
					*.h
 | 
				
			||||||
 | 
					/main
 | 
				
			||||||
 | 
					/Cargo.lock
 | 
				
			||||||
							
								
								
									
										16
									
								
								Cargo.toml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								Cargo.toml
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,16 @@
 | 
				
			|||||||
 | 
					[package]
 | 
				
			||||||
 | 
					name = "bdk_ffi"
 | 
				
			||||||
 | 
					version = "0.1.0"
 | 
				
			||||||
 | 
					authors = ["Steve Myers <steve@notmandatory.org>"]
 | 
				
			||||||
 | 
					edition = "2018"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
 | 
				
			||||||
 | 
					[lib]
 | 
				
			||||||
 | 
					crate-type = ["staticlib"]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					[dependencies]
 | 
				
			||||||
 | 
					bdk = { version = "^0.7", feature = ["all-keys"] }
 | 
				
			||||||
 | 
					safer-ffi = { version = "*", features = ["proc_macros"]}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					[features]
 | 
				
			||||||
 | 
					c-headers = ["safer-ffi/headers"]
 | 
				
			||||||
							
								
								
									
										13
									
								
								main.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								main.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,13 @@
 | 
				
			|||||||
 | 
					#include <stdlib.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "bdk_ffi.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int main (int argc, char const * const argv[])
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    Point_t * a = new_point(84,45);
 | 
				
			||||||
 | 
					    Point_t * b = new_point(0.0,39.0);
 | 
				
			||||||
 | 
					    Point_t * m = mid_point(a, b);
 | 
				
			||||||
 | 
					    print_point(m);
 | 
				
			||||||
 | 
					    print_point(NULL);
 | 
				
			||||||
 | 
					    return EXIT_SUCCESS;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										47
									
								
								src/lib.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								src/lib.rs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,47 @@
 | 
				
			|||||||
 | 
					use ::safer_ffi::prelude::*;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// A `struct` usable from both Rust and C
 | 
				
			||||||
 | 
					#[derive_ReprC]
 | 
				
			||||||
 | 
					#[repr(C)]
 | 
				
			||||||
 | 
					#[derive(Debug, Clone, Copy)]
 | 
				
			||||||
 | 
					pub struct Point {
 | 
				
			||||||
 | 
					    x: f64,
 | 
				
			||||||
 | 
					    y: f64,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* Export a Rust function to the C world. */
 | 
				
			||||||
 | 
					/// Returns the middle point of `[a, b]`.
 | 
				
			||||||
 | 
					#[ffi_export]
 | 
				
			||||||
 | 
					fn mid_point(a: Option<repr_c::Box<Point>>, b: Option<repr_c::Box<Point>>) -> repr_c::Box<Point> {
 | 
				
			||||||
 | 
					    let a = a.unwrap();
 | 
				
			||||||
 | 
					    let b = b.unwrap();
 | 
				
			||||||
 | 
					    repr_c::Box::new(Point {
 | 
				
			||||||
 | 
					        x: (a.x + b.x) / 2.,
 | 
				
			||||||
 | 
					        y: (a.y + b.y) / 2.,
 | 
				
			||||||
 | 
					    })
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Pretty-prints a point using Rust's formatting logic.
 | 
				
			||||||
 | 
					#[ffi_export]
 | 
				
			||||||
 | 
					fn print_point(point: Option<repr_c::Box<Point>>) {
 | 
				
			||||||
 | 
					    println!("{:?}", point);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[ffi_export]
 | 
				
			||||||
 | 
					fn new_point(x: f64, y: f64) -> repr_c::Box<Point> {
 | 
				
			||||||
 | 
					    repr_c::Box::new(Point { x, y })
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[ffi_export]
 | 
				
			||||||
 | 
					fn free_point(point: Option<repr_c::Box<Point>>) {
 | 
				
			||||||
 | 
					    drop(point)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// The following test function is necessary for the header generation.
 | 
				
			||||||
 | 
					#[::safer_ffi::cfg_headers]
 | 
				
			||||||
 | 
					#[test]
 | 
				
			||||||
 | 
					fn generate_headers() -> ::std::io::Result<()> {
 | 
				
			||||||
 | 
					    ::safer_ffi::headers::builder()
 | 
				
			||||||
 | 
					        .to_file("bdk_ffi.h")?
 | 
				
			||||||
 | 
					        .generate()
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user