[][src]Trait glsp::RGlobal

pub trait RGlobal: 'static {
    pub fn borrow() -> RGlobalRef<Self> { ... }
pub fn borrow_mut() -> RGlobalRefMut<Self> { ... }
pub fn try_borrow() -> Result<RGlobalRef<Self>, GError> { ... }
pub fn try_borrow_mut() -> Result<RGlobalRefMut<Self>, GError> { ... } }

A marker trait for global data.

You can implement this type for any 'static + Sized type, by writing:

impl RGlobal for MyType { }

This has three effects.

Firstly, it permits a single instance of MyType to be passed to glsp::add_rglobal, transferring ownership of that instance to the GameLisp runtime.

Secondly, it permits you to temporarily borrow that global instance of the type, by calling MyType::borrow() or MyType::borrow_mut().

Finally, it means that when you register a Rust function, parameters of type &T and &mut T will automatically borrow the global instance of MyType for the duration of the function. This makes it straightforward for you to define methods which can be called from both Rust and GameLisp.

struct Graphics {
	//...
}

impl RGlobal for Graphics { }

impl Graphics {
	fn draw_rect(&self, x: f32, y: f32, w: f32, h: f32, rgb: u32) -> GResult<()> {	
		/*
		the type of the &self parameter is &Graphics, so when this function is invoked
		from GameLisp, it will automatically call Graphics::borrow() to construct its
		&self argument.
		*/
	}
}

fn at_startup() {
	glsp::add_rglobal(Graphics::new())?;

	//there's no need for any manual conversions; we can just bind the method directly
	glsp::bind_rfn("draw-rect", &Graphics::draw_rect)?;

	//the method can now be called globally from GameLisp code
	eval!(
		"(draw-rect 10.0 10.0 64.0 64.0 0xff0000)"
	)?;
}

Provided methods

pub fn borrow() -> RGlobalRef<Self>[src]

Returns a shared reference to the global instance of this type owned by the active Runtime.

Panics if a global of this type has not been registered with the active Runtime, or if the global is currently mutably borrowed.

pub fn borrow_mut() -> RGlobalRefMut<Self>[src]

Returns a mutable reference to the global instance of this type owned by the active Runtime.

Panics if a global of this type has not been registered with the active Runtime, or if the global is currently borrowed.

pub fn try_borrow() -> Result<RGlobalRef<Self>, GError>[src]

Returns a shared reference to the global instance of this type owned by the active Runtime.

Returns an Err if a global of this type has not been registered with the active Runtime, or if the global is currently mutably borrowed.

pub fn try_borrow_mut() -> Result<RGlobalRefMut<Self>, GError>[src]

Returns a mutable reference to the global instance of this type owned by the active Runtime.

Returns an Err if a global of this type has not been registered with the active Runtime, or if the global is currently borrowed.

Loading content...

Implementors

Loading content...