fpagliughi / rust-industrial-io

Rust interface to the Linux Industrial I/O subsystem
MIT License
44 stars 21 forks source link

Missing function: create_context_from_uri #1

Closed knightshrub closed 5 years ago

knightshrub commented 5 years ago

Hello, the library currently misses the create_context_from_uri function. I need this in order to work with AD's ADALM-PLUTO SDR. I don't have a lot of experience using unsafe rust or doing ffi in rust, but would this be an acceptable implementation?

/// Tries to create a context from the specified URI
pub fn from_uri(uri: &str) -> Result<Context> {
    let uri = CString::new(uri).expect("CString::new(uri) failed");
    let ctx = unsafe { 
        ffi::iio_create_context_from_uri(uri.as_ptr()) 
     };
     if ctx.is_null() { bail!(SysError(Errno::last())); }
     Ok(Context{ ctx, })
}
fpagliughi commented 5 years ago

The first line is a bit problematic:

let uri = CString::new(uri).expect("CString::new(uri) failed");

Submit this as a Pull Request and I'll get it in shortly. I'll be back working on this library soon.

fpagliughi commented 5 years ago

Oh, and thanks. First issue for the library!

fpagliughi commented 5 years ago

Fixed with f7cb280f46f1812339035a21d84feca797e66aac