pub struct HostCallback { /* private fields */ }Expand description
A Rust function called on host arrays by a program (host_callback).
Compiled programs refer to it by an id (HostCallback::id), under
which it stays registered for the life of the process.
§Examples
use ruxla_ir::HostArray;
use ruxla_ir::callback::HostCallback;
let double = HostCallback::new("double", |args: &[HostArray]| {
let x = args[0].to_vec::<f32>().map_err(|e| e.to_string())?;
let y: Vec<f32> = x.iter().map(|v| 2.0 * v).collect();
Ok(vec![HostArray::from_vec(y, args[0].shape().clone())])
});
let out = double.call(&[HostArray::from_vec(vec![1.0f32, 2.0], [2])]).unwrap();
assert_eq!(out[0].to_vec::<f32>().unwrap(), [2.0, 4.0]);Implementations§
Source§impl HostCallback
impl HostCallback
Sourcepub fn new(
name: impl Into<String>,
f: impl Fn(&[HostArray]) -> Result<Vec<HostArray>, String> + Send + Sync + 'static,
) -> Self
pub fn new( name: impl Into<String>, f: impl Fn(&[HostArray]) -> Result<Vec<HostArray>, String> + Send + Sync + 'static, ) -> Self
A callback named name (for the printed IR and error messages).
Sourcepub fn id(&self) -> u64
pub fn id(&self) -> u64
The id under which compiled programs find the callback, registering it (for the life of the process) the first time.
Sourcepub fn lookup(id: u64) -> Option<Self>
pub fn lookup(id: u64) -> Option<Self>
The callback registered under id (HostCallback::id).
Trait Implementations§
Source§impl Clone for HostCallback
impl Clone for HostCallback
Source§impl Debug for HostCallback
impl Debug for HostCallback
Auto Trait Implementations§
impl !RefUnwindSafe for HostCallback
impl !UnwindSafe for HostCallback
impl Freeze for HostCallback
impl Send for HostCallback
impl Sync for HostCallback
impl Unpin for HostCallback
impl UnsafeUnpin for HostCallback
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more