Skip to content
Snippets Groups Projects
Commit 934e36ba authored by Sébastien Blin's avatar Sébastien Blin Committed by Adrien Béraud
Browse files

rust: ffi POC

parent 2e47b9ba
No related branches found
No related tags found
No related merge requests found
*.lock
target
[package]
name = "opendht"
version = "0.1.0"
authors = ["Sébastien Blin <sebastien.blin@savoirfairelinux.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
libc="0.2.0"
\ No newline at end of file
# Rust bindings for opendht
## Build
Install rust on your system (https://rustup.rs/ is the recommanded way). Once done:
```
cargo build
```
## Run tests
```
cargo test
```
\ No newline at end of file
extern crate libc;
use std::ffi::CString;
use libc::c_char;
#[repr(C)]
struct InfoHash {}
#[link(name = "opendht-c")]
extern {
fn dht_infohash_print(h: *const InfoHash) -> *mut c_char;
fn dht_infohash_random(h: *mut InfoHash) -> ();
}
impl InfoHash {
pub fn new() -> Box<InfoHash> {
let mut h = Box::new(InfoHash {});
unsafe {
dht_infohash_random(&mut *h);
}
h
}
pub fn print(&self) -> CString{
unsafe {
CString::from_raw(dht_infohash_print(&*self))
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn print_random_infohash() {
println!("{:?}", InfoHash::print(&*InfoHash::new()));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment