diff options
author | Chelsea Holland Komlo <me@chelseakomlo.com> | 2017-09-27 19:48:07 +0000 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2017-10-27 10:02:08 -0400 |
commit | d1820c1516a31a149fc51a9e5126bf899e4c4e08 (patch) | |
tree | ea2ed5c2259f87ca078378eff4f78c9bf0aa50da /src/rust/smartlist | |
parent | 5418aa84eec9f08af7ab7b68cbc9145750ad0a1b (diff) | |
download | tor-d1820c1516a31a149fc51a9e5126bf899e4c4e08.tar.gz tor-d1820c1516a31a149fc51a9e5126bf899e4c4e08.zip |
rust implementation of protover
Diffstat (limited to 'src/rust/smartlist')
-rw-r--r-- | src/rust/smartlist/Cargo.toml | 13 | ||||
-rw-r--r-- | src/rust/smartlist/lib.rs | 5 | ||||
-rw-r--r-- | src/rust/smartlist/smartlist.rs | 100 |
3 files changed, 118 insertions, 0 deletions
diff --git a/src/rust/smartlist/Cargo.toml b/src/rust/smartlist/Cargo.toml new file mode 100644 index 0000000000..51f486c4d7 --- /dev/null +++ b/src/rust/smartlist/Cargo.toml @@ -0,0 +1,13 @@ +[package] +authors = ["The Tor Project"] +version = "0.0.1" +name = "smartlist" + +[dependencies] +libc = "0.2.22" + +[lib] +name = "smartlist" +path = "lib.rs" +crate_type = ["rlib", "staticlib"] + diff --git a/src/rust/smartlist/lib.rs b/src/rust/smartlist/lib.rs new file mode 100644 index 0000000000..71d89a3b87 --- /dev/null +++ b/src/rust/smartlist/lib.rs @@ -0,0 +1,5 @@ +extern crate libc; + +mod smartlist; + +pub use smartlist::*; diff --git a/src/rust/smartlist/smartlist.rs b/src/rust/smartlist/smartlist.rs new file mode 100644 index 0000000000..5eff3fe43e --- /dev/null +++ b/src/rust/smartlist/smartlist.rs @@ -0,0 +1,100 @@ +use std::slice; +use libc::c_char; +use std::ffi::CStr; + +/// Smartlists are a type used in C code in tor to define a collection of a +/// generic type, which has a capacity and a number used. Each Smartlist +/// defines how to extract the list of values from the underlying C structure +/// Implementations are required to have a C representation +pub trait Smartlist<T> { + fn get_list(&self) -> Vec<T>; +} +#[repr(C)] +pub struct Stringlist { + pub list: *const *const c_char, + pub num_used: u8, + pub capacity: u8, +} + +impl Smartlist<String> for Stringlist { + fn get_list(&self) -> Vec<String> { + let empty: Vec<String> = Vec::new(); + let mut v: Vec<String> = Vec::new(); + + if self.list.is_null() { + return empty; + } + + // unsafe, as we need to extract the smartlist list into a vector of + // pointers, and then transform each element into a Rust string. + unsafe { + let elems = + slice::from_raw_parts(self.list, self.num_used as usize); + + for i in elems.iter() { + let c_str = CStr::from_ptr(*i); + let r_str = match c_str.to_str() { + Ok(n) => n, + Err(_) => return empty, + }; + v.push(String::from(r_str)); + } + } + + v + } +} + +#[cfg(test)] +mod test { + #[test] + fn test_get_list_of_strings() { + extern crate libc; + + use std::ffi::CString; + use libc::c_char; + + use super::Smartlist; + use super::Stringlist; + + { + // test to verify that null pointers are gracefully handled + use std::ptr; + + let sl = Stringlist { + list: ptr::null(), + num_used: 0, + capacity: 0, + }; + + let data = sl.get_list(); + assert_eq!(0, data.len()); + } + + { + let args = vec![String::from("a"), String::from("b")]; + + // for each string, transform it into a CString + let c_strings: Vec<_> = args.iter() + .map(|arg| CString::new(arg.as_str()).unwrap()) + .collect(); + + // then, collect a pointer for each CString + let p_args: Vec<_> = + c_strings.iter().map(|arg| arg.as_ptr()).collect(); + + // then, collect a pointer for the list itself + let p: *const *const c_char = p_args.as_ptr(); + + let sl = Stringlist { + list: p, + num_used: 2, + capacity: 2, + }; + + let data = sl.get_list(); + assert_eq!("a", &data[0]); + assert_eq!("b", &data[1]); + } + } +} |