Skip to content

Commit

Permalink
Add some docs. Tests for Value constants
Browse files Browse the repository at this point in the history
  • Loading branch information
Taaitaaiger committed Nov 22, 2020
1 parent dd8a15e commit 5ee47d1
Show file tree
Hide file tree
Showing 9 changed files with 453 additions and 12 deletions.
1 change: 1 addition & 0 deletions jl_sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ fn main() {
.whitelist_function("jl_init__threading")
.whitelist_function("jl_init_with_image__threading")
.whitelist_function("jl_is_initialized")
.whitelist_function("jl_isa")
.whitelist_function("jl_islayout_inline")
.whitelist_function("jl_new_array")
.whitelist_function("jl_new_struct_uninit")
Expand Down
1 change: 0 additions & 1 deletion jlrs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,6 @@ macro_rules! named_tuple {

pub mod error;
pub mod frame;
pub mod gc;
pub mod global;
#[doc(hidden)]
pub mod jl_sys_export;
Expand Down
2 changes: 2 additions & 0 deletions jlrs/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
pub mod bits_union;
pub mod cast;
pub mod frame;
pub mod gc;
pub mod into_julia;
pub mod julia_type;
pub mod julia_typecheck;
Expand All @@ -45,6 +46,7 @@ pub mod valid_layout;
pub use bits_union::{Align, BitsUnion, Flag};
pub use cast::Cast;
pub use frame::Frame;
pub use gc::Gc;
pub use into_julia::IntoJulia;
pub use julia_type::JuliaType;
pub use julia_typecheck::JuliaTypecheck;
Expand Down
2 changes: 2 additions & 0 deletions jlrs/src/traits/bits_union.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Machinery for bits unions.

/// Trait implemented by the aligning structs, which ensure bits unions are properly aligned.
/// Used in combination with `BitsUnion` and `Flag` to ensure bits unions are inserted correctly.
pub unsafe trait Align {
Expand Down
2 changes: 2 additions & 0 deletions jlrs/src/traits/cast.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Convert a `Value` to another type.

use crate::error::{JlrsError, JlrsResult};
use crate::value::Value;
use jl_sys::{
Expand Down
16 changes: 11 additions & 5 deletions jlrs/src/gc.rs → jlrs/src/traits/gc.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
use jl_sys::{jl_gc_collect, jl_gc_collection_t, jl_gc_enable, jl_gc_is_enabled};
//! Control the garbage collector.

use crate::traits::Frame;
use crate::Julia;
use jl_sys::{jl_gc_collect, jl_gc_collection_t, jl_gc_enable, jl_gc_is_enabled};

/// The different collection modes.
#[derive(Debug, Copy, Clone)]
pub enum GcCollection {
Auto = 0,
Full = 1,
Incremental = 2,
}

pub trait Gc: private::Sealed {
/// This trait is used to enable and disable the garbage collector and to force a collection.
pub trait Gc: private::Gc {
/// Enable or disable the GC.
unsafe fn enable_gc(&mut self, on: bool) -> bool {
jl_gc_enable(on as i32) == 1
}

/// Returns `true` if the GC is enabled.
fn gc_is_enabled(&mut self) -> bool {
unsafe { jl_gc_is_enabled() == 1 }
}

/// Force a collection.
unsafe fn gc_collect(&mut self, mode: GcCollection) {
jl_gc_collect(mode as jl_gc_collection_t)
}
Expand All @@ -29,7 +35,7 @@ impl<'frame, T: Frame<'frame>> Gc for T {}

mod private {
use super::{Frame, Julia};
pub trait Sealed {}
impl<'a, T: Frame<'a>> Sealed for T {}
impl Sealed for Julia {}
pub trait Gc {}
impl<'a, T: Frame<'a>> Gc for T {}
impl Gc for Julia {}
}
14 changes: 9 additions & 5 deletions jlrs/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ use jl_sys::{
jl_diverror_exception, jl_egal, jl_emptytuple, jl_eval_string, jl_exception_occurred, jl_false,
jl_field_index, jl_field_isptr, jl_field_names, jl_fieldref, jl_fieldref_noalloc, jl_finalize,
jl_gc_add_finalizer, jl_get_kwsorter, jl_get_nth_field, jl_get_nth_field_noalloc,
jl_interrupt_exception, jl_is_kind, jl_memory_exception, jl_new_array, jl_new_struct_uninit,
jl_new_structv, jl_nfields, jl_nothing, jl_object_id, jl_ptr_to_array, jl_ptr_to_array_1d,
jl_readonlymemory_exception, jl_set_nth_field, jl_stackovf_exception, jl_subtype, jl_svec_data,
jl_svec_len, jl_true, jl_type_union, jl_type_unionall, jl_typeof, jl_typeof_str,
jl_undefref_exception, jl_value_t,
jl_interrupt_exception, jl_is_kind, jl_isa, jl_memory_exception, jl_new_array,
jl_new_struct_uninit, jl_new_structv, jl_nfields, jl_nothing, jl_object_id, jl_ptr_to_array,
jl_ptr_to_array_1d, jl_readonlymemory_exception, jl_set_nth_field, jl_stackovf_exception,
jl_subtype, jl_svec_data, jl_svec_len, jl_true, jl_type_union, jl_type_unionall, jl_typeof,
jl_typeof_str, jl_undefref_exception, jl_value_t,
};
use std::borrow::BorrowMut;
use std::ffi::{CStr, CString};
Expand Down Expand Up @@ -653,6 +653,10 @@ impl<'frame, 'data> Value<'frame, 'data> {
pub fn object_id(self) -> usize {
unsafe { jl_object_id(self.ptr()) }
}

pub fn isa(self, other: Value) -> bool {
unsafe { jl_isa(self.ptr(), other.ptr()) != 0 }
}
}

/// # Type checking
Expand Down
2 changes: 1 addition & 1 deletion jlrs/tests/borrow_array_data.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use jlrs::gc::{Gc, GcCollection};
use jlrs::prelude::*;
use jlrs::traits::gc::{Gc, GcCollection};
use jlrs::util::JULIA;

macro_rules! impl_test {
Expand Down

0 comments on commit 5ee47d1

Please sign in to comment.