|
1 | | -use pecos_core::errors::PecosError; |
2 | | -use std::any::Any; |
3 | | -use std::fmt::Debug; |
| 1 | +// Re-export from pecos-wasm crate |
| 2 | +#[cfg(feature = "wasm")] |
| 3 | +pub use pecos_wasm::{DummyForeignObject, ForeignObject}; |
4 | 4 |
|
5 | | -/// Trait for foreign object implementations |
6 | | -pub trait ForeignObject: Debug + Send + Sync { |
7 | | - /// Clone the foreign object |
8 | | - fn clone_box(&self) -> Box<dyn ForeignObject>; |
9 | | - /// Initialize object before running a series of simulations |
10 | | - /// |
11 | | - /// # Errors |
12 | | - /// Returns an error if initialization fails. |
13 | | - fn init(&mut self) -> Result<(), PecosError>; |
14 | | - |
15 | | - /// Create new instance/internal state |
16 | | - /// |
17 | | - /// # Errors |
18 | | - /// Returns an error if instance creation fails. |
19 | | - fn new_instance(&mut self) -> Result<(), PecosError>; |
20 | | - |
21 | | - /// Get a list of function names available from the object |
22 | | - fn get_funcs(&self) -> Vec<String>; |
23 | | - |
24 | | - /// Execute a function given a list of arguments |
25 | | - /// |
26 | | - /// # Errors |
27 | | - /// Returns an error if the function does not exist or execution fails. |
28 | | - fn exec(&mut self, func_name: &str, args: &[i64]) -> Result<Vec<i64>, PecosError>; |
29 | | - |
30 | | - /// Cleanup resources |
31 | | - fn teardown(&mut self) {} |
32 | | - |
33 | | - /// Get as Any for downcasting |
34 | | - fn as_any(&self) -> &dyn Any; |
35 | | - |
36 | | - /// Get as Any for downcasting (mutable) |
37 | | - fn as_any_mut(&mut self) -> &mut dyn Any; |
38 | | -} |
39 | | - |
40 | | -/// Dummy foreign object for when no foreign object is needed |
41 | | -#[derive(Debug, Clone)] |
42 | | -pub struct DummyForeignObject {} |
43 | | - |
44 | | -impl DummyForeignObject { |
45 | | - /// Create a new dummy foreign object |
46 | | - #[must_use] |
47 | | - pub fn new() -> Self { |
48 | | - Self {} |
49 | | - } |
50 | | -} |
51 | | - |
52 | | -impl Default for DummyForeignObject { |
53 | | - fn default() -> Self { |
54 | | - Self::new() |
55 | | - } |
56 | | -} |
57 | | - |
58 | | -impl ForeignObject for DummyForeignObject { |
59 | | - fn clone_box(&self) -> Box<dyn ForeignObject> { |
60 | | - Box::new(Self::default()) |
61 | | - } |
62 | | - |
63 | | - fn init(&mut self) -> Result<(), PecosError> { |
64 | | - Ok(()) |
65 | | - } |
66 | | - |
67 | | - fn new_instance(&mut self) -> Result<(), PecosError> { |
68 | | - Ok(()) |
69 | | - } |
70 | | - |
71 | | - fn get_funcs(&self) -> Vec<String> { |
72 | | - vec![] |
73 | | - } |
74 | | - |
75 | | - fn exec(&mut self, func_name: &str, _args: &[i64]) -> Result<Vec<i64>, PecosError> { |
76 | | - Err(PecosError::Input(format!( |
77 | | - "Dummy foreign object cannot execute function: {func_name}" |
78 | | - ))) |
79 | | - } |
80 | | - |
81 | | - fn as_any(&self) -> &dyn Any { |
82 | | - self |
83 | | - } |
84 | | - |
85 | | - fn as_any_mut(&mut self) -> &mut dyn Any { |
86 | | - self |
87 | | - } |
88 | | -} |
| 5 | +// For when wasm feature is disabled, provide minimal trait |
| 6 | +#[cfg(not(feature = "wasm"))] |
| 7 | +pub use pecos_wasm::{DummyForeignObject, ForeignObject}; |
0 commit comments