Skip to content

Commit edb1a1b

Browse files
committed
implement refcounted byondvalue
1 parent 85d71bc commit edb1a1b

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

crates/byondapi-rs/src/value/functions.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ impl ByondValue {
233233
}
234234

235235
/// # Refcount operations
236+
/// DO NOT USE, use [`super::refcounted::RcByondValue`] instead
236237
impl ByondValue {
237238
pub fn increment_ref(&mut self) {
238239
unsafe { byond().ByondValue_IncRef(&self.0) }

crates/byondapi-rs/src/value/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use byondapi_sys::{ByondValueType, CByondValue};
55
use crate::static_global::byond;
66

77
/// [Newtype](https://doc.rust-lang.org/rust-by-example/generics/new_types.html) pattern over [`CByondValue`]
8+
/// WARNING: If this value is a ref created by byond passed to byondapi, it's a temp ref and will be deleted in a while
9+
/// Please convert it into a RcByondValue if you want byond to persist this ref
810
#[repr(transparent)]
911
#[derive(Clone, Copy)]
1012
pub struct ByondValue(pub CByondValue);
@@ -18,6 +20,7 @@ pub mod conversion;
1820
pub mod functions;
1921
pub mod list;
2022
pub mod pointer;
23+
pub mod refcounted;
2124
pub mod trait_impls;
2225
pub mod types;
2326

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use std::ops::{Deref, DerefMut};
2+
3+
use super::ByondValue;
4+
5+
/// Refcounted ByondValue, for refs you want rust to handle incrementing and decrementing.
6+
#[derive(Debug)]
7+
pub struct RcByondValue(ByondValue);
8+
9+
impl From<ByondValue> for RcByondValue {
10+
fn from(mut value: ByondValue) -> Self {
11+
value.increment_ref();
12+
RcByondValue(value)
13+
}
14+
}
15+
16+
impl Deref for RcByondValue {
17+
type Target = ByondValue;
18+
#[inline]
19+
fn deref(&self) -> &Self::Target {
20+
&self.0
21+
}
22+
}
23+
24+
impl DerefMut for RcByondValue {
25+
fn deref_mut(&mut self) -> &mut Self::Target {
26+
&mut self.0
27+
}
28+
}
29+
30+
impl Drop for RcByondValue {
31+
fn drop(&mut self) {
32+
self.0.decrement_ref();
33+
}
34+
}

0 commit comments

Comments
 (0)