nicer test for bless

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2020-11-25 12:23:34 +01:00
parent c837c7cc3b
commit ca00cfcc9a

View File

@ -1,25 +1,54 @@
#[perlmod::package(name = "RSPM::Bless", lib = "perlmod_test")] #[perlmod::package(name = "RSPM::Bless", lib = "perlmod_test")]
mod export { mod export {
use anyhow::Error; use anyhow::{format_err, Error};
use perlmod::Value; use perlmod::Value;
struct Bless {
content: String,
}
#[export(raw_return)] #[export(raw_return)]
fn new() -> Result<Value, Error> { fn new(#[raw] class: Value, content: String) -> Result<Value, Error> {
let hash = Value::from(perlmod::hash::Hash::new()); let mut ptr = Box::new(Bless { content });
let hash = Value::new_ref(&hash);
let hash = hash.bless("RSPM::Bless")?; let value = Value::new_pointer::<Bless>(&mut *ptr);
Ok(hash) let value = Value::new_ref(&value);
//Ok(this.bless("RSPM::Bless")?) let this = value.bless_sv(&class)?;
let _perl = Box::leak(ptr);
Ok(this)
} }
#[export] #[export]
fn something(#[raw] value: Value) { fn something(#[raw] value: Value) {
let _ = value; // ignore for now
println!("Called something!"); println!("Called something!");
} }
#[export] #[export]
fn DESTROY(#[raw] this: Value) { fn DESTROY(#[raw] this: Value) {
println!("Value dropped!"); match this
.dereference()
.ok_or_else(|| format_err!("not a reference"))
.and_then(|this| Ok(this.pv_raw()?))
{
Ok(ptr) => {
let value = unsafe { Box::<Bless>::from_raw(ptr) };
println!("Dropping value {:?}", value.content);
}
Err(err) => {
println!("DESTROY called with invalid pointer: {}", err);
} }
} }
}
}
// Example:
// use RSPM::Bless;
// my $foo = RSPM::Bless::new("Some Content");
// $foo->something(); // works
//
// output:
// Called something!
// Dropping value "Some Content"