|
| 1 | +#![no_std] |
| 2 | +#![feature(core_intrinsics, lang_items)] |
| 3 | + |
| 4 | +#[macro_use] |
| 5 | +extern crate intrusive_splay_tree; |
| 6 | + |
| 7 | +use core::cmp::Ordering; |
| 8 | +use core::mem; |
| 9 | +use core::ptr; |
| 10 | + |
| 11 | +pub use intrusive_splay_tree::SplayTree; |
| 12 | + |
| 13 | +// Need to provide a tiny `panic_fmt` lang-item implementation for `#![no_std]`. |
| 14 | +// This implementation will translate panics into traps in the resulting |
| 15 | +// WebAssembly. |
| 16 | +#[lang = "panic_fmt"] |
| 17 | +extern "C" fn panic_fmt( |
| 18 | + _args: ::core::fmt::Arguments, |
| 19 | + _file: &'static str, |
| 20 | + _line: u32 |
| 21 | +) -> ! { |
| 22 | + use core::intrinsics; |
| 23 | + unsafe { |
| 24 | + intrinsics::abort(); |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)] |
| 29 | +pub struct Id(pub u32); |
| 30 | + |
| 31 | +#[derive(Debug)] |
| 32 | +pub struct Monster<'a> { |
| 33 | + id: Id, |
| 34 | + health: u32, |
| 35 | + by_id_node: intrusive_splay_tree::Node<'a>, |
| 36 | + by_health_node: intrusive_splay_tree::Node<'a>, |
| 37 | +} |
| 38 | + |
| 39 | +pub struct MonstersById; |
| 40 | + |
| 41 | +impl_intrusive_node! { |
| 42 | + impl<'a> IntrusiveNode<'a> for MonstersById |
| 43 | + where |
| 44 | + type Elem = Monster<'a>, |
| 45 | + node = by_id_node; |
| 46 | +} |
| 47 | + |
| 48 | +impl<'a> intrusive_splay_tree::TreeOrd<'a, MonstersById> for Monster<'a> { |
| 49 | + fn tree_cmp(&self, rhs: &Monster<'a>) -> Ordering { |
| 50 | + self.id.cmp(&rhs.id) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +pub struct MonstersByHealth; |
| 55 | +impl_intrusive_node! { |
| 56 | + impl<'a> IntrusiveNode<'a> for MonstersByHealth |
| 57 | + where |
| 58 | + type Elem = Monster<'a>, |
| 59 | + node = by_health_node; |
| 60 | +} |
| 61 | +impl<'a> intrusive_splay_tree::TreeOrd<'a, MonstersByHealth> for Monster<'a> { |
| 62 | + fn tree_cmp(&self, rhs: &Monster<'a>) -> Ordering { |
| 63 | + self.health.cmp(&rhs.health) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +impl<'a> intrusive_splay_tree::TreeOrd<'a, MonstersByHealth> for u32 { |
| 68 | + fn tree_cmp(&self, rhs: &Monster<'a>) -> Ordering { |
| 69 | + self.cmp(&rhs.health) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +impl<'a> intrusive_splay_tree::TreeOrd<'a, MonstersById> for Id { |
| 74 | + fn tree_cmp(&self, rhs: &Monster<'a>) -> Ordering { |
| 75 | + self.cmp(&rhs.id) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +extern "C" { |
| 80 | + fn alloc(n: usize) -> *mut u8; |
| 81 | +} |
| 82 | + |
| 83 | +#[no_mangle] |
| 84 | +pub unsafe extern "C" fn new_id_tree() -> *mut SplayTree<'static, MonstersById> { |
| 85 | + let p = alloc(mem::size_of::<SplayTree<MonstersById>>()); |
| 86 | + let p = p as *mut SplayTree<MonstersById>; |
| 87 | + ptr::write(p, SplayTree::default()); |
| 88 | + p |
| 89 | +} |
| 90 | + |
| 91 | +#[no_mangle] |
| 92 | +pub unsafe extern "C" fn new_health_tree() -> *mut SplayTree<'static, MonstersByHealth> { |
| 93 | + let p = alloc(mem::size_of::<SplayTree<MonstersByHealth>>()); |
| 94 | + let p = p as *mut SplayTree<MonstersByHealth>; |
| 95 | + ptr::write(p, SplayTree::default()); |
| 96 | + p |
| 97 | +} |
| 98 | + |
| 99 | +#[no_mangle] |
| 100 | +pub unsafe extern "C" fn new_monster( |
| 101 | + id: u32, |
| 102 | + health: u32, |
| 103 | + by_id_tree: *mut SplayTree<'static, MonstersById>, |
| 104 | + by_health_tree: *mut SplayTree<'static, MonstersByHealth>, |
| 105 | +) -> *const Monster<'static> { |
| 106 | + let p = alloc(mem::size_of::<Monster>()); |
| 107 | + let p = p as *mut Monster<'static>; |
| 108 | + ptr::write( |
| 109 | + p, |
| 110 | + Monster { |
| 111 | + id: Id(id), |
| 112 | + health, |
| 113 | + by_id_node: Default::default(), |
| 114 | + by_health_node: Default::default(), |
| 115 | + }, |
| 116 | + ); |
| 117 | + let monster = &*p; |
| 118 | + (*by_id_tree).insert(monster); |
| 119 | + (*by_health_tree).insert(monster); |
| 120 | + monster |
| 121 | +} |
| 122 | + |
| 123 | +#[no_mangle] |
| 124 | +pub unsafe extern "C" fn query_by_id( |
| 125 | + tree: *mut SplayTree<'static, MonstersById>, |
| 126 | + id: u32, |
| 127 | +) -> *const Monster<'static> { |
| 128 | + (*tree).find(&Id(id)).map_or(ptr::null(), |m| m as *const _) |
| 129 | +} |
| 130 | + |
| 131 | +#[no_mangle] |
| 132 | +pub unsafe extern "C" fn query_by_health( |
| 133 | + tree: *mut SplayTree<'static, MonstersByHealth>, |
| 134 | + health: u32, |
| 135 | +) -> *const Monster<'static> { |
| 136 | + (*tree).find(&health).map_or(ptr::null(), |m| m as *const _) |
| 137 | +} |
0 commit comments