Trait jlrs::memory::scope::Scope[][src]

pub trait Scope<'target, 'current, 'data, F>: Sized + Scope<'target, 'current, 'data, F> where
    F: Frame<'current>, 
{ fn value_scope<G>(self, func: G) -> JlrsResult<Self::Value>
    where
        G: for<'nested, 'inner> FnOnce(Output<'target>, &'inner mut GcFrame<'nested, F::Mode>) -> JlrsResult<OutputValue<'target, 'data, 'inner>>
;
fn result_scope<G>(self, func: G) -> JlrsResult<Self::JuliaResult>
    where
        G: for<'nested, 'inner> FnOnce(Output<'target>, &'inner mut GcFrame<'nested, F::Mode>) -> JlrsResult<OutputResult<'target, 'data, 'inner>>
;
fn value_scope_with_slots<G>(
        self,
        capacity: usize,
        func: G
    ) -> JlrsResult<Self::Value>
    where
        G: for<'nested, 'inner> FnOnce(Output<'target>, &'inner mut GcFrame<'nested, F::Mode>) -> JlrsResult<OutputValue<'target, 'data, 'inner>>
;
fn result_scope_with_slots<G>(
        self,
        capacity: usize,
        func: G
    ) -> JlrsResult<Self::JuliaResult>
    where
        G: for<'nested, 'inner> FnOnce(Output<'target>, &'inner mut GcFrame<'nested, F::Mode>) -> JlrsResult<OutputResult<'target, 'data, 'inner>>
; fn global(&self) -> Global<'target> { ... } }
Expand description

Trait that provides methods to create nested scopes which eventually root a value in the frame of a target scope. It’s implemented for OutputScope and mutable references to implementors of Frame. In addition to nesting, many methods that allocate a new value take an implementation of this trait as their first argument. If a mutable reference to a frame is used this way, the value is rooted in that frame. If it’s an OutputScope, it’s rooted in the frame for which the Output was originally created.

Required methods

Create a new GcFrame that can be used to root capacity values, an Output for the current scope, and use them to call the inner closure. The final result is not rooted in this newly created frame, but the current frame. The final result must not be the result of a function call, use Scope::result_scope for that purpose instead. If the current scope is a mutable reference to a frame, calling this method will require one slot of the current frame.

Example:

  julia.scope(|global, frame| {
      let _nt = frame.value_scope(|output, frame| {
          let v1 = Value::new(&mut *frame, 1usize)?;
          let v2 = Value::new(&mut *frame, 2usize)?;

          let output = output.into_scope(frame);
          named_tuple!(output, "a" => v1, "b" => v2)
      })?;

      Ok(())
  }).unwrap();

Create a new GcFrame that can be used to root capacity values, an Output for the current scope, and use them to call the inner closure. The final result is not rooted in this newly created frame, but the current frame. The final result must be the result of a function call, if you want to create a new value use Scope::value_scope instead. If the current scope is a mutable reference to a frame, calling this method will require one slot of the current frame.

Example:

  julia.scope(|global, frame| {
      let sum = frame.result_scope(|output, frame| {
          let v1 = Value::new(&mut *frame, 1usize)?;
          let v2 = Value::new(&mut *frame, 2usize)?;

          let add = Module::base(global).function(&mut *frame, "+")?;

          let output = output.into_scope(frame);
          unsafe { add.call2(output, v1, v2) }
      })?.unwrap().unbox::<usize>()?;

      assert_eq!(sum, 3);
      Ok(())
  }).unwrap();

Create a new GcFrame that can be used to root capacity values, an Output for the current scope, and use them to call the inner closure. The final result is not rooted in this newly created frame, but the current frame. The final result must not be the result of a function call, use Scope::result_scope for that purpose instead. If the current scope is a mutable reference to a frame, calling this method will require one slot of the current frame.

Example:

  julia.scope(|global, frame| {
      let _nt = frame.value_scope(|output, frame| {
          let v1 = Value::new(&mut *frame, 1usize)?;
          let v2 = Value::new(&mut *frame, 2usize)?;

          let output = output.into_scope(frame);
          named_tuple!(output, "a" => v1, "b" => v2)
      })?;

      Ok(())
  }).unwrap();

Create a new GcFrame that can be used to root capacity values, an Output for the current scope, and use them to call the inner closure. The final result is not rooted in this newly created frame, but the current frame. The final result must be the result of a function call, if you want to create a new value use Scope::value_scope instead. If the current scope is a mutable reference to a frame, calling this method will require one slot of the current frame.

Example:

  julia.scope(|global, frame| {
      let sum = frame.result_scope(|output, frame| {
          let v1 = Value::new(&mut *frame, 1usize)?;
          let v2 = Value::new(&mut *frame, 2usize)?;

          let add = Module::base(global).function(&mut *frame, "+")?;

          let output = output.into_scope(frame);
          unsafe { add.call2(output, v1, v2) }
      })?.unwrap().unbox::<usize>()?;

      assert_eq!(sum, 3);
      Ok(())
  }).unwrap();

Provided methods

Create a new Global.

Implementations on Foreign Types

Creates a GcFrame and calls the given closure with it. Returns the result of this closure.

Example:

julia.scope(|_global, frame| {
    let _nt = frame.value_scope(|output, frame| {
        let i = Value::new(&mut *frame, 2u64)?;
        let j = Value::new(&mut *frame, 1u32)?;
    
        let output = output.into_scope(frame);
        named_tuple!(output, "i" => i, "j" => j)
    })?;

    Ok(())
}).unwrap();

Creates a GcFrame and calls the given closure with it. Returns the result of this closure.

Example:

  julia.scope(|global, frame| {
      let sum = frame.result_scope(|output, frame| {
          let v1 = Value::new(&mut *frame, 1usize)?;
          let v2 = Value::new(&mut *frame, 2usize)?;

          let func = Module::base(global)
              .function(&mut *frame, "+")?;

          let output = output.into_scope(frame);
          unsafe { func.call2(output, v1, v2) }
      })?.unwrap()
          .unbox::<usize>()?;

      assert_eq!(sum, 3);
      Ok(())
  }).unwrap();

Creates a GcFrame and calls the given closure with it. Returns the result of this closure.

Example:

julia.scope(|_global, frame| {
    let _nt = frame.value_scope_with_slots(2, |output, frame| {
        let i = Value::new(&mut *frame, 2u64)?;
        let j = Value::new(&mut *frame, 1u32)?;
    
        let output = output.into_scope(frame);
        named_tuple!(output, "i" => i, "j" => j)
    })?;

    Ok(())
}).unwrap();

Creates a GcFrame with capacity preallocated slots and calls the given closure with it. Returns the result of this closure.

Example:

  julia.scope(|global, frame| {
      let sum = frame.result_scope_with_slots(2, |output, frame| {
          let v1 = Value::new(&mut *frame, 1usize)?;
          let v2 = Value::new(&mut *frame, 2usize)?;


          let func = Module::base(global)
              .function(&mut *frame, "+")?;

          let output = output.into_scope(frame);
          unsafe { func.call2(output, v1, v2) }
      })?.unwrap()
          .unbox::<usize>()?;

      assert_eq!(sum, 3);
      Ok(())
  }).unwrap();

Implementors