Container を書く
整数の値を一つ持つ Container と、その値に引数を足す ContainerProcess を例にします。
Container
Section titled “Container”use juiz_sdk::prelude::*;
#[repr(Rust)]pub struct ExampleContainer { pub value: i64}
#[juiz_container]fn example_container(initial_value: i64) -> JuizResult<Box<ExampleContainer>> { Ok(Box::new(ExampleContainer { value: initial_value }))}#pragma once#include <cstdint>
class CppContainer {public: int64_t value; CppContainer(int64_t v) : value(v) {}};#include "juiz/juiz.h"#include "example_container.h"
juiz::Value manifest() { return ContainerManifest("example_container_cpp").into_value();}
CppContainer* create_container(juiz::Value value) { int64_t int_value = 0; if (value.isObjectValue() && value.hasKey("value")) { auto v = value.objectValue()["value"]; if (v.isIntValue()) int_value = v.intValue(); } return new CppContainer(int_value);}
bool destroy_container(CppContainer* p_container) { if (p_container) { delete p_container; return true; } return false;}
CONTAINER_FACTORY(manifest, create_container, destroy_container);from juiz import *
class PyContainer: value: int def __init__(self, value): self.value = value
@juiz_containerdef example_container_python(initial_value: int = 0) -> PyContainer: return PyContainer(initial_value)ContainerProcess
Section titled “ContainerProcess”ContainerProcess は、最初の引数に Container を受け取る Process です。container_type で結びつける Container の型名を指定します。
use example_container::ExampleContainer;use juiz_sdk::prelude::*;
#[juiz_container_process(container_type = "example_container")]fn increment_function(container: &mut ContainerImpl<ExampleContainer>, arg1: i64) -> JuizResult<Capsule> { container.value = container.value + arg1; return Ok(jvalue!(container.value).into());}#include "juiz/juiz.h"#include "example_container.h"
juiz::Value manifest() { return ProcessManifest("example_container_cpp_increment") .container_type("example_container_cpp") .add_int_arg("arg0", "test_argument", 2) .into_value();}
std::optional<int64_t> example_container_increment(CppContainer* container, juiz::CapsuleMap cm) { int64_t v = cm.get_int("arg0"); container->value = container->value + v; return container->value;}
CONTAINER_PROCESS_FACTORY(CppContainer, manifest, example_container_increment)from juiz import juiz_container_process
@juiz_container_process(container_type="example_container_python")def example_container_python_get(container) -> int: return container.value単体で動かす
Section titled “単体で動かす”juiz --container ./target/debug/libexample_container.so \ --container_process ./target/debug/libexample_container_increment.so \ -l rust -e -1 -d