let reply_id = TRAIT_REPLY_COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
let (tx, rx) = tokio::sync::oneshot::channel::<Result<String, String>>();
TRAIT_REPLY_CHANNELS.lock().unwrap().insert(reply_id, tx);

let pid = self.inner;

{%- for arg in native_args %}
let {{ arg.binding }} = {{ arg.owned_expr }};
{%- endfor %}

let method = "{{ method_name }}";

drop(tokio::task::spawn_blocking(move || {
    let mut env = rustler::OwnedEnv::new();
    let _ = env.send_and_clear(&pid, |env| {
        let {% if native_args %}mut {% endif %}args_map = rustler::Term::map_new(env);
{%- for arg in native_args %}
        args_map = args_map.map_put("{{ arg.key }}".encode(env), {{ arg.binding }}.encode(env)).expect("map_put");
{%- endfor %}
        (rustler::types::atom::Atom::from_str(env, "trait_call").unwrap(), method, args_map, reply_id).encode(env)
    });
}));

match rx.blocking_recv() {
{%- if has_error %}
    Ok(Ok(json)) => serde_json::from_str(&json).map_err(|_e| {{ error_deser }}),
    Ok(Err(msg)) => Err({{ error_msg }}),
    Err(_) => Err({{ error_closed }})
{%- else %}
    Ok(Ok(json)) => serde_json::from_str(&json).unwrap_or_default(),
    _ => Default::default()
{%- endif %}
}
