[][src]Trait jlrs::traits::JuliaStruct

pub unsafe trait JuliaStruct: JuliaType + IntoJulia + Copy + Clone { }

In order to be able to create Julia structs from Rust and convert a Value that contains one back to Rust you will need to derive this trait. This has the following requirements:

  • the struct must be a struct with named fields.
  • the names of the fields must match.
  • The associated Julia type must be explicitly provided
  • the struct must be annotated with #[repr(C)].
  • the struct must implement Copy and Clone.
  • the struct must only contain fields that implement JuliaType and IntoJulia.

If all these requirements are satisfied this trait can be derived successfully and corresponds to a Julia struct with the same name and fields. If you have the following struct in Julia

struct MyStruct
    a::UInt64
    b::Int32
end

in the Main module, you can derive JuliaStruct like this:

This example is not tested
#[derive(Copy, Clone, JuliaStruct)]
#[repr(C)]
#[jlrs(julia_type = "Main.MyStruct")]
struct MyStruct {
    a: u64,   
    b: i32,   
}

When you derive this trait, three additional traits are derived: JuliaType, IntoJulia, and JuliaTypecheck. As a result, this struct can be used in combination with DataType::is, Value::is, Value::new, and Value::cast.

If you want or need to use another name for a field, you can use the rename-attribute:

This example is not tested
#[derive(Copy, Clone, JuliaStruct)]
#[jlrs(julia_type = "Main.🍔")]
#[repr(C)]
struct Hamburger {
    #[jlrs(rename = "🥒")]
    pickle: i32,
    #[jlrs(rename = "🍅")]
    tomato: f32,
}

Implementors

Loading content...