Struct Plugin

Source
pub struct Plugin<State> { /* private fields */ }

Implementations§

Source§

impl<State> Plugin<State>

Source

pub fn new(state: State, fullpath: &str) -> Box<Plugin<State>>

Examples found in repository?
examples/basic_host.rs (line 18)
12fn main() {
13    let mut plugin_name = env::current_exe().expect("Failed to get current path");
14    println!("Path of this executable is: {}", plugin_name.display());
15    plugin_name.set_file_name("libbasic_guest.so");
16    // build the libbasic_guest.so file from the samples of cr.h
17    println!("Call cr_plugin_load(ctx, {:?})", plugin_name);
18    let mut plugin = BasicPlugin::new(BasicState { counter: 0 }, plugin_name.to_str().unwrap());
19
20    let mut err_cnt = 0;
21    loop {
22        println!("Run Update:");
23        let rc = plugin.update(true);
24        println!("cr_plugin_update(ctx, true) = {}", rc);
25        if rc != 0 {
26            println!("Plugin error: {:?}", plugin.get_failure());
27            err_cnt += 1;
28            if err_cnt > 10 {
29                break;
30            }
31        }
32        thread::sleep(Duration::from_millis(200));
33    }
34    println!("Call cr_plugin_close(ctx)");
35    drop(plugin);
36    println!("exit");
37}
More examples
Hide additional examples
examples/basic_guest.rs (line 21)
15pub fn plugin_main(ctx: &mut BasicPlugin, cr_op: cr::cr_op) -> i32 {
16    // Test "guest" feature.
17    #[cfg(not(feature = "guest"))]
18    {
19        println!("Guest compiled with host-side code.");
20        // This code will only run if the "guest" feature is not used.
21        let plugin = BasicPlugin::new(BasicState { counter: 0 }, "test");
22        println!("- plugin = {:?}", plugin);
23    }
24
25    println!("test recompile. test1");
26    //panic!("test");
27    // Test crash.
28    // Rollback to the previous version seems to work once.
29    // Then on the next reload it gets stuck in a setjmp/longjmp loop?
30    /*
31    unsafe {
32        let ptr
33            = std::ptr::null_mut() as *mut std::os::raw::c_int;
34        // invalid read.
35        println!("Read from null pointer: {:?}", *ptr);
36        // invalid write.
37        *ptr = 0x1234;
38    }
39    // */
40    match cr_op {
41        CR_LOAD => {
42            println!("Plugin load. version = {}", ctx.get_version());
43        }
44        CR_STEP => {
45            let version = ctx.get_version();
46            let state = ctx.state_mut();
47            state.counter += 1;
48            println!(
49                "Plugin step. count = {}. version = {}",
50                state.counter, version
51            );
52
53            // slow down the printing.
54            thread::sleep(Duration::from_millis(200));
55        }
56        CR_UNLOAD => {
57            println!("Plugin unload. version = {}", ctx.get_version());
58        }
59        CR_CLOSE => {
60            println!("Plugin close. version = {}", ctx.get_version());
61        }
62    }
63
64    return 0;
65}
Source

pub fn from_ctx(ctx: &mut cr_plugin) -> &mut Plugin<State>

Source

pub fn set_temporary_path(&mut self, path: &str)

Source

pub fn update(&mut self, reload_check: bool) -> i32

Examples found in repository?
examples/basic_host.rs (line 23)
12fn main() {
13    let mut plugin_name = env::current_exe().expect("Failed to get current path");
14    println!("Path of this executable is: {}", plugin_name.display());
15    plugin_name.set_file_name("libbasic_guest.so");
16    // build the libbasic_guest.so file from the samples of cr.h
17    println!("Call cr_plugin_load(ctx, {:?})", plugin_name);
18    let mut plugin = BasicPlugin::new(BasicState { counter: 0 }, plugin_name.to_str().unwrap());
19
20    let mut err_cnt = 0;
21    loop {
22        println!("Run Update:");
23        let rc = plugin.update(true);
24        println!("cr_plugin_update(ctx, true) = {}", rc);
25        if rc != 0 {
26            println!("Plugin error: {:?}", plugin.get_failure());
27            err_cnt += 1;
28            if err_cnt > 10 {
29                break;
30            }
31        }
32        thread::sleep(Duration::from_millis(200));
33    }
34    println!("Call cr_plugin_close(ctx)");
35    drop(plugin);
36    println!("exit");
37}
Source

pub fn get_version(&self) -> u32

Examples found in repository?
examples/basic_guest.rs (line 42)
15pub fn plugin_main(ctx: &mut BasicPlugin, cr_op: cr::cr_op) -> i32 {
16    // Test "guest" feature.
17    #[cfg(not(feature = "guest"))]
18    {
19        println!("Guest compiled with host-side code.");
20        // This code will only run if the "guest" feature is not used.
21        let plugin = BasicPlugin::new(BasicState { counter: 0 }, "test");
22        println!("- plugin = {:?}", plugin);
23    }
24
25    println!("test recompile. test1");
26    //panic!("test");
27    // Test crash.
28    // Rollback to the previous version seems to work once.
29    // Then on the next reload it gets stuck in a setjmp/longjmp loop?
30    /*
31    unsafe {
32        let ptr
33            = std::ptr::null_mut() as *mut std::os::raw::c_int;
34        // invalid read.
35        println!("Read from null pointer: {:?}", *ptr);
36        // invalid write.
37        *ptr = 0x1234;
38    }
39    // */
40    match cr_op {
41        CR_LOAD => {
42            println!("Plugin load. version = {}", ctx.get_version());
43        }
44        CR_STEP => {
45            let version = ctx.get_version();
46            let state = ctx.state_mut();
47            state.counter += 1;
48            println!(
49                "Plugin step. count = {}. version = {}",
50                state.counter, version
51            );
52
53            // slow down the printing.
54            thread::sleep(Duration::from_millis(200));
55        }
56        CR_UNLOAD => {
57            println!("Plugin unload. version = {}", ctx.get_version());
58        }
59        CR_CLOSE => {
60            println!("Plugin close. version = {}", ctx.get_version());
61        }
62    }
63
64    return 0;
65}
Source

pub fn get_failure(&self) -> cr_failure

Examples found in repository?
examples/basic_host.rs (line 26)
12fn main() {
13    let mut plugin_name = env::current_exe().expect("Failed to get current path");
14    println!("Path of this executable is: {}", plugin_name.display());
15    plugin_name.set_file_name("libbasic_guest.so");
16    // build the libbasic_guest.so file from the samples of cr.h
17    println!("Call cr_plugin_load(ctx, {:?})", plugin_name);
18    let mut plugin = BasicPlugin::new(BasicState { counter: 0 }, plugin_name.to_str().unwrap());
19
20    let mut err_cnt = 0;
21    loop {
22        println!("Run Update:");
23        let rc = plugin.update(true);
24        println!("cr_plugin_update(ctx, true) = {}", rc);
25        if rc != 0 {
26            println!("Plugin error: {:?}", plugin.get_failure());
27            err_cnt += 1;
28            if err_cnt > 10 {
29                break;
30            }
31        }
32        thread::sleep(Duration::from_millis(200));
33    }
34    println!("Call cr_plugin_close(ctx)");
35    drop(plugin);
36    println!("exit");
37}
Source

pub fn state(&self) -> &State

Source

pub fn state_mut(&mut self) -> &mut State

Examples found in repository?
examples/basic_guest.rs (line 46)
15pub fn plugin_main(ctx: &mut BasicPlugin, cr_op: cr::cr_op) -> i32 {
16    // Test "guest" feature.
17    #[cfg(not(feature = "guest"))]
18    {
19        println!("Guest compiled with host-side code.");
20        // This code will only run if the "guest" feature is not used.
21        let plugin = BasicPlugin::new(BasicState { counter: 0 }, "test");
22        println!("- plugin = {:?}", plugin);
23    }
24
25    println!("test recompile. test1");
26    //panic!("test");
27    // Test crash.
28    // Rollback to the previous version seems to work once.
29    // Then on the next reload it gets stuck in a setjmp/longjmp loop?
30    /*
31    unsafe {
32        let ptr
33            = std::ptr::null_mut() as *mut std::os::raw::c_int;
34        // invalid read.
35        println!("Read from null pointer: {:?}", *ptr);
36        // invalid write.
37        *ptr = 0x1234;
38    }
39    // */
40    match cr_op {
41        CR_LOAD => {
42            println!("Plugin load. version = {}", ctx.get_version());
43        }
44        CR_STEP => {
45            let version = ctx.get_version();
46            let state = ctx.state_mut();
47            state.counter += 1;
48            println!(
49                "Plugin step. count = {}. version = {}",
50                state.counter, version
51            );
52
53            // slow down the printing.
54            thread::sleep(Duration::from_millis(200));
55        }
56        CR_UNLOAD => {
57            println!("Plugin unload. version = {}", ctx.get_version());
58        }
59        CR_CLOSE => {
60            println!("Plugin close. version = {}", ctx.get_version());
61        }
62    }
63
64    return 0;
65}

Trait Implementations§

Source§

impl<State: Debug> Debug for Plugin<State>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<State> Drop for Plugin<State>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<State> Freeze for Plugin<State>
where State: Freeze,

§

impl<State> RefUnwindSafe for Plugin<State>
where State: RefUnwindSafe,

§

impl<State> !Send for Plugin<State>

§

impl<State> !Sync for Plugin<State>

§

impl<State> Unpin for Plugin<State>
where State: Unpin,

§

impl<State> UnwindSafe for Plugin<State>
where State: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.