| 308 | } |
| 309 | |
| 310 | pub(crate) fn create(&self, ty: MemoryType, initial_len: usize) -> Result<MemoryStorage> { |
| 311 | let storage = match &self.kind { |
| 312 | MemoryBackendKind::Vec => { |
| 313 | Box::new(VecMemory::try_new(initial_len).map_err(Error::Trap)?) as Box<dyn LinearMemory> |
| 314 | } |
| 315 | MemoryBackendKind::Paged { chunk_size } => { |
| 316 | Box::new(PagedMemory::try_new(initial_len, *chunk_size).map_err(Error::Trap)?) as Box<dyn LinearMemory> |
| 317 | } |
| 318 | MemoryBackendKind::Custom(factory) => factory(ty)?, |
| 319 | }; |
| 320 | |
| 321 | if storage.len() < initial_len { |
| 322 | return Err(Error::Other(format!( |
| 323 | "memory backend returned {} bytes for a memory that requires at least {initial_len}", |
| 324 | storage.len() |
| 325 | ))); |
| 326 | } |
| 327 | |
| 328 | Ok(MemoryStorage(storage)) |
| 329 | } |
| 330 | |
| 331 | pub(crate) fn create_lazy(&self, ty: MemoryType, initial_len: usize) -> Result<MemoryStorage> { |
| 332 | Ok(MemoryStorage(Box::new(LazyLinearMemory::new_with_initial_len(ty, initial_len, self.clone())))) |