File tree Expand file tree Collapse file tree 3 files changed +38
-0
lines changed
crates/byondapi-rs/src/value Expand file tree Collapse file tree 3 files changed +38
-0
lines changed Original file line number Diff line number Diff line change @@ -233,6 +233,7 @@ impl ByondValue {
233233}
234234
235235/// # Refcount operations
236+ /// DO NOT USE, use [`super::refcounted::RcByondValue`] instead
236237impl ByondValue {
237238 pub fn increment_ref ( & mut self ) {
238239 unsafe { byond ( ) . ByondValue_IncRef ( & self . 0 ) }
Original file line number Diff line number Diff line change @@ -5,6 +5,8 @@ use byondapi_sys::{ByondValueType, CByondValue};
55use 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 ) ]
1012pub struct ByondValue ( pub CByondValue ) ;
@@ -18,6 +20,7 @@ pub mod conversion;
1820pub mod functions;
1921pub mod list;
2022pub mod pointer;
23+ pub mod refcounted;
2124pub mod trait_impls;
2225pub mod types;
2326
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments