Skip to content

Commit

Permalink
reserve this
Browse files Browse the repository at this point in the history
  • Loading branch information
jupyterkat committed Mar 30, 2024
1 parent b1a04ed commit d35de22
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
4 changes: 3 additions & 1 deletion crates/byondapi-rs/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,16 @@ pub fn byond_block(corner1: ByondXYZ, corner2: ByondXYZ) -> Result<Vec<ByondValu
}

BUFFER.with_borrow_mut(|buff| -> Result<Vec<ByondValue>, Error> {
let initial_len = buff.capacity() as u32;
let mut len = buff.capacity() as u32;
// Safety: buffer capacity is passed to byond, which makes sure it writes in-bound
let initial_res = unsafe {
byond().Byond_Block(&corner1.0, &corner2.0, buff.as_mut_ptr().cast(), &mut len)
};
match (initial_res, len) {
(false, 1..) => {
buff.reserve_exact(len as usize);
debug_assert!(len > initial_len);
buff.reserve_exact((len - initial_len) as usize);
// Safety: buffer capacity is passed to byond, which makes sure it writes in-bound
unsafe {
map_byond_error!(byond().Byond_Block(
Expand Down
4 changes: 3 additions & 1 deletion crates/byondapi-rs/src/value/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ impl ByondValue {
}

let bytes = BUFFER.with_borrow_mut(|buff| -> Result<Vec<u8>, Error> {
let initial_len = buff.capacity() as u32;
let mut len = buff.capacity() as u32;
// Safety: buffer capacity is passed to byond, which makes sure it writes in-bound
let initial_res =
unsafe { byond().Byond_ToString(&self.0, buff.as_mut_ptr().cast(), &mut len) };
match (initial_res, len) {
(false, 1..) => {
buff.reserve_exact(len as usize);
debug_assert!(len > initial_len);
buff.reserve_exact((len - initial_len) as usize);
// Safety: buffer capacity is passed to byond, which makes sure it writes in-bound
unsafe {
map_byond_error!(byond().Byond_ToString(
Expand Down
8 changes: 6 additions & 2 deletions crates/byondapi-rs/src/value/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ impl ByondValue {
}

BUFFER.with_borrow_mut(|buff| -> Result<Vec<ByondValue>, Error> {
let initial_len = buff.capacity() as u32;
let mut len = buff.capacity() as u32;

// Safety: buffer capacity is passed to byond, which makes sure it writes in-bound
let initial_res =
unsafe { byond().Byond_ReadList(&self.0, buff.as_mut_ptr().cast(), &mut len) };
match (initial_res, len) {
(false, 1..) => {
buff.reserve_exact(len as usize);
debug_assert!(len > initial_len);
buff.reserve_exact((len - initial_len) as usize);
// Safety: buffer capacity is passed to byond, which makes sure it writes in-bound
unsafe {
map_byond_error!(byond().Byond_ReadList(
Expand Down Expand Up @@ -56,14 +58,16 @@ impl ByondValue {
}

BUFFER.with_borrow_mut(|buff| -> Result<Vec<ByondValue>, Error> {
let initial_len = buff.capacity() as u32;
let mut len = buff.capacity() as u32;

// Safety: buffer capacity is passed to byond, which makes sure it writes in-bound
let initial_res =
unsafe { byond().Byond_ReadListAssoc(&self.0, buff.as_mut_ptr().cast(), &mut len) };
match (initial_res, len) {
(false, 1..) => {
buff.reserve_exact(len as usize);
debug_assert!(len > initial_len);
buff.reserve_exact((len - initial_len) as usize);
// Safety: buffer capacity is passed to byond, which makes sure it writes in-bound
unsafe {
map_byond_error!(byond().Byond_ReadListAssoc(
Expand Down

0 comments on commit d35de22

Please sign in to comment.