smartlist.rs 3.2 KB

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