Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::collections::HashMap;
- use std::mem;
- use std::vec::Vec;
- #[derive(PartialEq, Eq, Debug)]
- pub struct Schema {
- arena: Vec<SchemaItem<'static>>,
- map: HashMap<String, &'static SchemaItem<'static>>,
- }
- impl Schema {
- fn get<'a>(&'a self, ty: &str) -> &'a SchemaItem<'a> {
- unsafe {
- mem::transmute::<&'a SchemaItem<'static>, &'a SchemaItem<'a>>(
- self.map.get(ty).unwrap(),
- )
- }
- }
- }
- #[derive(PartialEq, Eq, Debug)]
- pub enum SchemaItem<'a> {
- Bool,
- I8,
- I16,
- I32,
- I64,
- I128,
- U8,
- U16,
- U32,
- U64,
- U128,
- F32,
- F64,
- Char,
- String,
- ByteArray,
- Option(&'a SchemaItem<'a>),
- Unit,
- UnitStruct,
- UnitVariant,
- NewtypeStruct,
- NewtypeVariant,
- Seq(&'a SchemaItem<'a>),
- Tuple(Vec<&'a SchemaItem<'a>>),
- TupleStruct(Vec<&'a SchemaItem<'a>>),
- TupleVariant(Vec<&'a SchemaItem<'a>>),
- Map(&'a SchemaItem<'a>, &'a SchemaItem<'a>),
- Struct(HashMap<String, &'a SchemaItem<'a>>),
- StructVariant(HashMap<String, &'a SchemaItem<'a>>),
- }
- impl Schema
- {
- fn new() -> Self
- {
- let mut schema = Schema
- {
- arena: Vec::with_capacity(2),
- map: HashMap::new()
- };
- schema.arena.push(SchemaItem::Bool);
- schema.arena.push(SchemaItem::Bool);
- schema.map.insert("one".to_owned(), &schema.arena[0]);
- schema.map.insert("two".to_owned(), &schema.arena[1]);
- unsafe
- {
- *(&schema.arena[1] as *const SchemaItem as *mut SchemaItem) = SchemaItem::Option(&schema.arena[0]);
- }
- schema
- }
- }
- fn main()
- {
- let james = Schema::new();
- }
Add Comment
Please, Sign In to add comment