mirror of
https://git.proxmox.com/git/perlmod
synced 2025-10-05 20:36:33 +00:00
56 lines
1.4 KiB
Rust
56 lines
1.4 KiB
Rust
#[perlmod::package(name = "RSPM::Bless", lib = "perlmod_test")]
|
|
mod export {
|
|
use std::convert::TryFrom;
|
|
|
|
use anyhow::Error;
|
|
|
|
use perlmod::Value;
|
|
|
|
const CLASSNAME: &str = "RSPM::Bless";
|
|
|
|
struct Bless {
|
|
content: String,
|
|
}
|
|
|
|
#[export(raw_return)]
|
|
fn new(#[raw] class: Value, content: String) -> Result<Value, Error> {
|
|
let mut ptr = Box::new(Bless { content });
|
|
|
|
let value = Value::new_pointer::<Bless>(&mut *ptr);
|
|
let value = Value::new_ref(&value);
|
|
let this = value.bless_sv(&class)?;
|
|
let _perl = Box::leak(ptr);
|
|
|
|
Ok(this)
|
|
}
|
|
|
|
#[export]
|
|
fn something(#[raw] this: Value) -> Result<(), Error> {
|
|
let this = unsafe { this.from_blessed_box::<Bless>(CLASSNAME)? };
|
|
println!("Called something on Bless {{ {:?} }}!", this.content);
|
|
Ok(())
|
|
}
|
|
|
|
#[export]
|
|
fn another(#[try_from_ref] this: &Bless, param: u32) -> Result<(), Error> {
|
|
println!(
|
|
"Called 'another({})' on Bless {{ {:?} }}!",
|
|
param, this.content
|
|
);
|
|
Ok(())
|
|
}
|
|
|
|
#[export(name = "DESTROY")]
|
|
fn destroy(#[raw] this: Value) {
|
|
perlmod::destructor!(this, Bless: CLASSNAME);
|
|
}
|
|
|
|
impl<'a> TryFrom<&'a Value> for &'a Bless {
|
|
type Error = Error;
|
|
|
|
fn try_from(value: &'a Value) -> Result<&'a Bless, Error> {
|
|
Ok(unsafe { value.from_blessed_box(CLASSNAME)? })
|
|
}
|
|
}
|
|
}
|