diff options
author | Alex Orlenko <zxteam@protonmail.com> | 2021-05-31 23:33:44 +0100 |
---|---|---|
committer | Alex Orlenko <zxteam@protonmail.com> | 2021-06-03 16:42:55 +0100 |
commit | a944f4ad6f1d3ae379abf53bc846e6d7ee360f83 (patch) | |
tree | 441c558d61648742a53d6aeda8a74a3b0240afac /tests | |
parent | bae424672a02d6d7abd9099ab3eab9e972a7556e (diff) | |
download | mlua-a944f4ad6f1d3ae379abf53bc846e6d7ee360f83.zip |
Implement `UserData` for Rc<RefCell>/Arc<Mutex>/Arc<RwLock> wrappers
Diffstat (limited to 'tests')
-rw-r--r-- | tests/userdata.rs | 55 |
1 files changed, 54 insertions, 1 deletions
diff --git a/tests/userdata.rs b/tests/userdata.rs index 601fa91..e88e97b 100644 --- a/tests/userdata.rs +++ b/tests/userdata.rs @@ -1,4 +1,7 @@ -use std::sync::Arc; +use std::sync::{Arc, Mutex, RwLock}; + +#[cfg(not(feature = "send"))] +use std::{cell::RefCell, rc::Rc}; #[cfg(feature = "lua54")] use std::sync::atomic::{AtomicI64, Ordering}; @@ -451,3 +454,53 @@ fn test_metatable() -> Result<()> { Ok(()) } + +#[test] +fn test_userdata_wrapped() -> Result<()> { + struct MyUserData(i64); + + impl UserData for MyUserData { + fn add_fields<'lua, F: UserDataFields<'lua, Self>>(fields: &mut F) { + fields.add_field_method_get("data", |_, this| Ok(this.0)); + fields.add_field_method_set("data", |_, this, val| { + this.0 = val; + Ok(()) + }) + } + } + + let lua = Lua::new(); + let globals = lua.globals(); + + #[cfg(not(feature = "send"))] + { + globals.set("rc_refcell_ud", Rc::new(RefCell::new(MyUserData(1))))?; + lua.load( + r#" + rc_refcell_ud.data = rc_refcell_ud.data + 1 + assert(rc_refcell_ud.data == 2) + "#, + ) + .exec()?; + } + + globals.set("arc_mutex_ud", Arc::new(Mutex::new(MyUserData(2))))?; + lua.load( + r#" + arc_mutex_ud.data = arc_mutex_ud.data + 1 + assert(arc_mutex_ud.data == 3) + "#, + ) + .exec()?; + + globals.set("arc_rwlock_ud", Arc::new(RwLock::new(MyUserData(3))))?; + lua.load( + r#" + arc_rwlock_ud.data = arc_rwlock_ud.data + 1 + assert(arc_rwlock_ud.data == 4) + "#, + ) + .exec()?; + + Ok(()) +} |