Guest User

Untitled

a guest
Jun 25th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. use std::collections::HashMap;
  2. use std::mem;
  3. use std::vec::Vec;
  4.  
  5. #[derive(PartialEq, Eq, Debug)]
  6. pub struct Schema {
  7. arena: Vec<SchemaItem<'static>>,
  8. map: HashMap<String, &'static SchemaItem<'static>>,
  9. }
  10.  
  11. impl Schema {
  12. fn get<'a>(&'a self, ty: &str) -> &'a SchemaItem<'a> {
  13. unsafe {
  14. mem::transmute::<&'a SchemaItem<'static>, &'a SchemaItem<'a>>(
  15. self.map.get(ty).unwrap(),
  16. )
  17. }
  18. }
  19. }
  20.  
  21. #[derive(PartialEq, Eq, Debug)]
  22. pub enum SchemaItem<'a> {
  23. Bool,
  24. I8,
  25. I16,
  26. I32,
  27. I64,
  28. I128,
  29. U8,
  30. U16,
  31. U32,
  32. U64,
  33. U128,
  34. F32,
  35. F64,
  36. Char,
  37. String,
  38. ByteArray,
  39. Option(&'a SchemaItem<'a>),
  40. Unit,
  41. UnitStruct,
  42. UnitVariant,
  43. NewtypeStruct,
  44. NewtypeVariant,
  45. Seq(&'a SchemaItem<'a>),
  46. Tuple(Vec<&'a SchemaItem<'a>>),
  47. TupleStruct(Vec<&'a SchemaItem<'a>>),
  48. TupleVariant(Vec<&'a SchemaItem<'a>>),
  49. Map(&'a SchemaItem<'a>, &'a SchemaItem<'a>),
  50. Struct(HashMap<String, &'a SchemaItem<'a>>),
  51. StructVariant(HashMap<String, &'a SchemaItem<'a>>),
  52. }
  53.  
  54. impl Schema
  55. {
  56. fn new() -> Self
  57. {
  58. let mut schema = Schema
  59. {
  60. arena: Vec::with_capacity(2),
  61. map: HashMap::new()
  62. };
  63.  
  64. schema.arena.push(SchemaItem::Bool);
  65. schema.arena.push(SchemaItem::Bool);
  66.  
  67. schema.map.insert("one".to_owned(), &schema.arena[0]);
  68. schema.map.insert("two".to_owned(), &schema.arena[1]);
  69.  
  70. unsafe
  71. {
  72. *(&schema.arena[1] as *const SchemaItem as *mut SchemaItem) = SchemaItem::Option(&schema.arena[0]);
  73. }
  74.  
  75. schema
  76. }
  77. }
  78.  
  79. fn main()
  80. {
  81. let james = Schema::new();
  82. }
Add Comment
Please, Sign In to add comment