pub fn call_with_callbacks(
    id: CanisterId,
    method: &str,
    data: &[u8],
    reply: impl FnOnce() + 'static,
    reject: impl FnOnce() + 'static,
) -> i32 {
    type Closures = (Box<dyn FnOnce() + 'static>, Box<dyn FnOnce() + 'static>);
    fn on_reply(env: *mut ()) {
        let closure = unsafe { Box::from_raw(env as *mut Closures) }.0;
        closure();
    }
    fn on_reject(env: *mut ()) {
        let closure = unsafe { Box::from_raw(env as *mut Closures) }.1;
        closure();
    }
    let boxed_closures: Box<Closures> = Box::new((Box::new(reply), Box::new(reject)));
    let env = Box::into_raw(boxed_closures);

    let err_code = call_raw(
        id,
        method,
        data,
        on_reply,
        on_reject,
        None,
        env as *mut (),
        Funds::zero(),
    );

    if err_code != 0 {
        // deallocate the closures
        let _ = unsafe { Box::from_raw(env as *mut Closures) };
    }

    err_code
}
/// Calls another canister and executes one of the callbacks.
Calls another canister and executes one of the callbacks.
Calls another canister and executes one of the callbacks .
pub fn call_with_callbacks(
    id: CanisterId,
    method: &str,
    data: &[u8],
    reply: impl FnOnce() + 'static,
    reject: impl FnOnce() + 'static,
) -> i32 {
    type Closures = (Box<dyn FnOnce() + 'static>, Box<dyn FnOnce() + 'static>);
    fn on_reply(env: *mut ()) {
        let closure = unsafe { Box::from_raw(env as *mut Closures) }.0;
        closure();
    }
    fn on_reject(env: *mut ()) {
        let closure = unsafe { Box::from_raw(env as *mut Closures) }.1;
        closure();
    }
    let boxed_closures: Box<Closures> = Box::new((Box::new(reply), Box::new(reject)));
    let env = Box::into_raw(boxed_closures);
    let err_code = call_raw(
        id,
        method,
        data,
        on_reply,
        on_reject,
        None,
        env as *mut (),
        Funds::zero(),
    );
    if err_code != 0 {
        // deallocate the closures
        let _ = unsafe { Box::from_raw(env as *mut Closures) };
    }
    err_code
}
pub fn call_with_callbacks ( id : CanisterId , method : & str , data : & [ u8 ] , reply : impl FnOnce ( ) + ' static , reject : impl FnOnce ( ) + ' static , ) -> i32 { type Closures = ( Box < dyn FnOnce ( ) + ' static > , Box < dyn FnOnce ( ) + ' static > ) ; fn on_reply ( env : * mut ( ) ) { let closure = unsafe { Box :: from_raw ( env as * mut Closures ) } . 0 ; closure ( ) ; } fn on_reject ( env : * mut ( ) ) { let closure = unsafe { Box :: from_raw ( env as * mut Closures ) } . 1 ; closure ( ) ; } let boxed_closures : Box < Closures > = Box :: new ( ( Box :: new ( reply ) , Box :: new ( reject ) ) ) ; let env = Box :: into_raw ( boxed_closures ) ; let err_code = call_raw ( id , method , data , on_reply , on_reject , None , env as * mut ( ) , Funds :: zero ( ) , ) ; if err_code != 0 { // deallocate the closures let _ = unsafe { Box :: from_raw ( env as * mut Closures ) } ; } err_code }
Calls another canister and executes one of the callbacks.
Calls another canister and executes one of the callbacks .
id
CanisterId
method
&str
data
&[u8]
reply
impl FnOnce() + 'static
reject
impl FnOnce() + 'static
id
CanisterId
method
&str
data
&[u8]
reply
impl FnOnce() + 'static
reject
impl FnOnce() + 'static