[][src]Macro glsp::rdata

macro_rules! rdata {
    (
		$(#[$struct_attr:meta])*
		$struct_vis:vis struct $rdata:ident { $($struct_token:tt)* }
		$($rest:tt)*
	) => { ... };
    (
		$(#[$struct_attr:meta])*
		$struct_vis:vis struct $rdata:ident;
		$($rest:tt)*
	) => { ... };
    (
		$(#[$struct_attr:meta])*
		$struct_vis:vis struct $rdata:ident ( $($struct_token:tt)* );
		$($rest:tt)*
	) => { ... };
}

Defines a struct which can be stored on the garbage-collected heap.

The input must be a struct declaration, optionally followed by a meths { ... } block. The macro defines that struct, implements the RStore trait for the struct's type, implements MakeArg for shared and mutable references to the struct's type, and implements IntoResult for the struct's value type.

rdata! {
	#[derive(Clone)]
	struct AudioClip {
		samples: Vec<i16>,
		channels: Channels
	}

	meths {
		get "duration": AudioClip::duration,
		"play": AudioClip::play
	}
}

impl AudioClip {
	fn load<P: AsRef<Path>>(path: P) -> AudioClip {
		//...
	}

	fn duration(&self) -> usize {
		self.samples.len() / self.channels.count()
	}

	fn play(&self, mixer: &mut Mixer) {
		mixer.play_audio_clip(self);
	}
}

glsp::bind_rfn("AudioClip:load", AudioClip::load::<PathBuf>)?;

When a reference to an rdata! struct is bound as an RFn parameter, that parameter expects an argument which belongs to the rdata primitive type. That argument is borrowed for the duration of the function call.

The meths block contains a comma-separated list of "name": fn_expr pairs. Each fn_expr is passed to the rfn! macro, and the resulting WrappedFn is bound as a method which can be called by GameLisp code.

Each "name" can be prefixed with the keyword get or set to bind it as a property getter or property setter, respectively.