|
3 | 3 |
|
4 | 4 | //! Implementations of the custom traits for the Rust collection types. |
5 | 5 |
|
6 | | -use std::{borrow::Cow, collections::BTreeMap}; |
| 6 | +use std::{ |
| 7 | + borrow::Cow, |
| 8 | + collections::{BTreeMap, BTreeSet}, |
| 9 | +}; |
7 | 10 |
|
8 | 11 | use frunk::HList; |
9 | 12 |
|
|
95 | 98 | entries.lower(memory) |
96 | 99 | } |
97 | 100 | } |
| 101 | + |
| 102 | +impl<T> WitType for BTreeSet<T> |
| 103 | +where |
| 104 | + T: WitType, |
| 105 | +{ |
| 106 | + const SIZE: u32 = <Vec<T> as WitType>::SIZE; |
| 107 | + |
| 108 | + type Layout = <Vec<T> as WitType>::Layout; |
| 109 | + type Dependencies = HList![T]; |
| 110 | + |
| 111 | + fn wit_type_name() -> Cow<'static, str> { |
| 112 | + <Vec<T> as WitType>::wit_type_name() |
| 113 | + } |
| 114 | + |
| 115 | + fn wit_type_declaration() -> Cow<'static, str> { |
| 116 | + <Vec<T> as WitType>::wit_type_declaration() |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +impl<T> WitLoad for BTreeSet<T> |
| 121 | +where |
| 122 | + T: WitType + Ord + WitLoad, |
| 123 | +{ |
| 124 | + fn load<Instance>( |
| 125 | + memory: &Memory<'_, Instance>, |
| 126 | + location: GuestPointer, |
| 127 | + ) -> Result<Self, RuntimeError> |
| 128 | + where |
| 129 | + Instance: InstanceWithMemory, |
| 130 | + <Instance::Runtime as Runtime>::Memory: RuntimeMemory<Instance>, |
| 131 | + { |
| 132 | + let entries = <Vec<T> as WitLoad>::load(memory, location)?; |
| 133 | + Ok(entries.into_iter().collect()) |
| 134 | + } |
| 135 | + |
| 136 | + fn lift_from<Instance>( |
| 137 | + flat_layout: <Self::Layout as Layout>::Flat, |
| 138 | + memory: &Memory<'_, Instance>, |
| 139 | + ) -> Result<Self, RuntimeError> |
| 140 | + where |
| 141 | + Instance: InstanceWithMemory, |
| 142 | + <Instance::Runtime as Runtime>::Memory: RuntimeMemory<Instance>, |
| 143 | + { |
| 144 | + let entries = <Vec<T> as WitLoad>::lift_from(flat_layout, memory)?; |
| 145 | + Ok(entries.into_iter().collect()) |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +impl<T> WitStore for BTreeSet<T> |
| 150 | +where |
| 151 | + T: WitType + WitStore, |
| 152 | + for<'a> &'a T: WitStore, |
| 153 | +{ |
| 154 | + fn store<Instance>( |
| 155 | + &self, |
| 156 | + memory: &mut Memory<'_, Instance>, |
| 157 | + location: GuestPointer, |
| 158 | + ) -> Result<(), RuntimeError> |
| 159 | + where |
| 160 | + Instance: InstanceWithMemory, |
| 161 | + <Instance::Runtime as Runtime>::Memory: RuntimeMemory<Instance>, |
| 162 | + { |
| 163 | + let entries = self.iter().collect::<Vec<&T>>(); |
| 164 | + entries.store(memory, location) |
| 165 | + } |
| 166 | + |
| 167 | + fn lower<Instance>( |
| 168 | + &self, |
| 169 | + memory: &mut Memory<'_, Instance>, |
| 170 | + ) -> Result<Self::Layout, RuntimeError> |
| 171 | + where |
| 172 | + Instance: InstanceWithMemory, |
| 173 | + <Instance::Runtime as Runtime>::Memory: RuntimeMemory<Instance>, |
| 174 | + { |
| 175 | + let entries = self.iter().collect::<Vec<&T>>(); |
| 176 | + entries.lower(memory) |
| 177 | + } |
| 178 | +} |
0 commit comments