daily pastebin goal
3%
SHARE
TWEET

Untitled

a guest Jul 21st, 2018 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use std::string::String as StdString;
  2. use std::mem;
  3.  
  4. #[derive(Copy, Clone)]
  5. #[repr(C)]
  6. pub enum ValueType {
  7.     Nil = 0,
  8.     Int = 1,
  9.     String = 2,
  10. }
  11.  
  12. #[repr(C)]
  13. pub struct Value {
  14.     tag: ValueType,
  15.     value: ValueVariant,
  16. }
  17.  
  18. #[repr(C)]
  19. pub union ValueVariant {
  20.     nil: Nil,
  21.     int: i64,
  22.     string: String,
  23. }
  24.  
  25. #[derive(Copy, Clone)]
  26. #[repr(C)]
  27. pub struct Nil {}
  28.  
  29. #[derive(Copy, Clone)]
  30. #[repr(C)]
  31. pub struct String {
  32.     ptr: *mut u8,
  33.     length: usize,
  34.     capacity: usize,
  35. }
  36.  
  37. impl From<::Value> for Value {
  38.     fn from(value: ::Value) -> Self {
  39.         match value {
  40.             ::Value::Nil => Value { tag: ValueType::Nil, value: ValueVariant { nil: Nil {} } },
  41.             ::Value::Int(n) => Value { tag: ValueType::Int, value: ValueVariant { int: n } },
  42.             ::Value::String(s) => {
  43.                 let length = s.len();
  44.                 let capacity = s.capacity();
  45.                 let ptr = s.as_ptr() as *mut u8;
  46.                 mem::forget(s);
  47.  
  48.                 Value {
  49.                     tag: ValueType::String,
  50.                     value: ValueVariant {
  51.                         string: String {
  52.                             ptr,
  53.                             length,
  54.                             capacity,
  55.                         }
  56.                     }
  57.                 }
  58.             },
  59.         }
  60.     }
  61. }
  62.  
  63. unsafe extern fn rust_butterfly_value_free(value: Value) {
  64.     match value.tag {
  65.         ValueType::Nil => {},
  66.         ValueType::Int => {},
  67.         ValueType::String => {
  68.             let string = value.value.string;
  69.             StdString::from_raw_parts(string.ptr, string.length, string.capacity);
  70.         }
  71.     }
  72. }
  73.  
  74. // unsafe extern fn rust_butterfly_value_call(name: *const u8, args: *const Value) -> Value {
  75.  
  76. // }
  77.  
  78. // pub extern fn rust_butterfly_call()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top