Skip to content

Commit

Permalink
use Vec's try_reserve_exact method (#3656)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvduin authored Oct 13, 2023
1 parent 84b735c commit cb64552
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ description = """
Easy support for interacting between JS and Rust.
"""
edition = "2018"
rust-version = "1.56"
rust-version = "1.57"

[package.metadata.docs.rs]
features = ["serde-serialize"]
Expand Down
22 changes: 2 additions & 20 deletions src/externref.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use crate::JsValue;
use std::alloc::{self, Layout};
use std::cell::Cell;
use std::mem;
use std::ptr;
use std::slice;
use std::vec::Vec;
use std::cmp::max;
Expand Down Expand Up @@ -46,23 +43,8 @@ impl Slab {
internal_error("someone else allocated table entries?")
}

// poor man's `try_reserve_exact` until that's stable
unsafe {
let new_cap = self.data.capacity() + extra;
let size = mem::size_of::<usize>() * new_cap;
let align = mem::align_of::<usize>();
let layout = match Layout::from_size_align(size, align) {
Ok(l) => l,
Err(_) => internal_error("size/align layout failure"),
};
let ptr = alloc::alloc(layout) as *mut usize;
if ptr.is_null() {
internal_error("allocation failure");
}
ptr::copy_nonoverlapping(self.data.as_ptr(), ptr, self.data.len());
let new_vec = Vec::from_raw_parts(ptr, self.data.len(), new_cap);
let mut old = mem::replace(&mut self.data, new_vec);
old.set_len(0);
if self.data.try_reserve_exact(extra).is_err() {
internal_error("allocation failure");
}
}

Expand Down

0 comments on commit cb64552

Please sign in to comment.