smartlist.rs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. use std::slice;
  2. use libc::{c_char, c_int};
  3. use std::ffi::CStr;
  4. /// Smartlists are a type used in C code in tor to define a collection of a
  5. /// generic type, which has a capacity and a number used. Each Smartlist
  6. /// defines how to extract the list of values from the underlying C structure
  7. ///
  8. /// Implementations are required to have a C representation, as this module
  9. /// serves purely to translate smartlists as defined in tor to vectors in Rust.
  10. pub trait Smartlist<T> {
  11. fn get_list(&self) -> Vec<T>;
  12. }
  13. #[repr(C)]
  14. pub struct Stringlist {
  15. pub list: *const *const c_char,
  16. pub num_used: c_int,
  17. pub capacity: c_int,
  18. }
  19. impl Smartlist<String> for Stringlist {
  20. fn get_list(&self) -> Vec<String> {
  21. let empty: Vec<String> = Vec::new();
  22. let mut rust_list: Vec<String> = Vec::new();
  23. if self.list.is_null() || self.num_used == 0 {
  24. return empty;
  25. }
  26. // unsafe, as we need to extract the smartlist list into a vector of
  27. // pointers, and then transform each element into a Rust string.
  28. let elems: &[*const i8] =
  29. unsafe { slice::from_raw_parts(self.list, self.num_used as usize) };
  30. for elem in elems.iter() {
  31. if elem.is_null() {
  32. continue;
  33. }
  34. // unsafe, as we need to create a cstring from the referenced
  35. // element
  36. let c_string = unsafe { CStr::from_ptr(*elem) };
  37. let r_string = match c_string.to_str() {
  38. Ok(n) => n,
  39. Err(_) => return empty,
  40. };
  41. rust_list.push(String::from(r_string));
  42. }
  43. rust_list
  44. }
  45. }
  46. // TODO: CHK: this module maybe should be tested from a test in C with a
  47. // smartlist as defined in tor.
  48. #[cfg(test)]
  49. mod test {
  50. #[test]
  51. fn test_get_list_of_strings() {
  52. extern crate libc;
  53. use std::ffi::CString;
  54. use libc::c_char;
  55. use super::Smartlist;
  56. use super::Stringlist;
  57. {
  58. // test to verify that null pointers are gracefully handled
  59. use std::ptr;
  60. let sl = Stringlist {
  61. list: ptr::null(),
  62. num_used: 0,
  63. capacity: 0,
  64. };
  65. let data = sl.get_list();
  66. assert_eq!(0, data.len());
  67. }
  68. {
  69. let args = vec![String::from("a"), String::from("b")];
  70. // for each string, transform it into a CString
  71. let c_strings: Vec<_> = args.iter()
  72. .map(|arg| CString::new(arg.as_str()).unwrap())
  73. .collect();
  74. // then, collect a pointer for each CString
  75. let p_args: Vec<_> =
  76. c_strings.iter().map(|arg| arg.as_ptr()).collect();
  77. let p: *const *const c_char = p_args.as_ptr();
  78. // This is the representation that we expect when receiving a
  79. // smartlist at the Rust/C FFI layer.
  80. let sl = Stringlist {
  81. list: p,
  82. num_used: 2,
  83. capacity: 2,
  84. };
  85. let data = sl.get_list();
  86. assert_eq!("a", &data[0]);
  87. assert_eq!("b", &data[1]);
  88. }
  89. }
  90. }