perlmod: add Value::new_xsub as an unsafe fn

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2021-12-17 09:17:09 +01:00
parent af31038b48
commit 25d21384ae

View File

@ -6,9 +6,9 @@ use std::fmt;
use serde::{Deserialize, Serialize};
use crate::ffi::{self, SV};
use crate::raw_value;
use crate::scalar::ScalarRef;
use crate::Error;
use crate::{perl_fn, raw_value};
use crate::{Array, Hash, Scalar};
/// A higher level value. This is basically an [`SV`] already cast to [`AV`](crate::ffi::AV) or
@ -52,6 +52,23 @@ impl Value {
Value::Scalar(Scalar::new_bytes(s))
}
/// Create a new reference code reference.
///
/// # Safety
///
/// It is up to the user to ensure that only a valid perl XSUB is used. You should know what
/// you're doing, as perl code WILL execute the function and expect it to behave in a
/// particular way!
pub unsafe fn new_xsub(xsub: perl_fn!(extern "C" fn(*mut crate::ffi::CV))) -> Self {
Self::from_raw_move(crate::ffi::RSPL_newXS_flags(
std::ptr::null(),
xsub as _,
std::ptr::null(),
std::ptr::null(),
0,
) as _)
}
/// If the value is an array, returns the associated [`Array`].
pub fn as_array(&self) -> Option<&Array> {
match self {