lib.rs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. extern crate zry_rust_ffi_utils;
  2. use zry_rust_ffi_utils::bytes_vec::ZRFUBytesVec;
  3. use zry_rust_ffi_utils::caller::ProcCallerLibSide;
  4. use zry_rust_ffi_utils::proc::{ZRFUProcArgs, ZRFUProcInstanceTrait, ZRFUProcReturn};
  5. use zry_rust_ffi_utils::proc::ZRFUProcResultCode;
  6. use serde::{Serialize, Deserialize};
  7. use ciborium::{cbor, Value};
  8. use zry_rust_ffi_utils::{proc_def_ar, proc_def_a, proc_def_r, proc_def_n};
  9. use zrfu_macro::zrfu_proc_def;
  10. #[no_mangle]
  11. pub extern "C" fn LibWithZRFU_Init(caller: &dyn ProcCallerLibSide){
  12. caller.register_proc(String::from("TestFuncAR"), TestProcAR::factory);
  13. caller.register_proc(String::from("TestFuncA"), TestProcA::factory);
  14. caller.register_proc(String::from("TestFuncR"), TestProcR::factory);
  15. caller.register_proc(String::from("TestFuncN"), TestProcN::factory);
  16. }
  17. #[derive(Debug, Serialize, Deserialize)]
  18. struct TestProcCborIn {
  19. a: String,
  20. b: i64,
  21. c: Value,
  22. d: Value,
  23. }
  24. #[derive(Debug, Serialize, Deserialize)]
  25. struct TestProcCborOut {
  26. a: String,
  27. b: i64,
  28. c: Value,
  29. d: Value,
  30. }
  31. zrfu_proc_def!{
  32. proc fn TestProcAR (argsdata: TestProcCborIn) -> (retdata: TestProcCborOut) {
  33. println!("hello, this function '{}' has args and return.", self.proc_name.as_str());
  34. println!("args data: {:?}", argsdata);
  35. let retdata = TestProcCborOut{
  36. a: argsdata.a.clone(),
  37. b: argsdata.b,
  38. c: argsdata.c.clone(),
  39. d: argsdata.d.clone(),
  40. };
  41. }
  42. }
  43. zrfu_proc_def!{
  44. proc fn TestProcA (argsdata: TestProcCborIn) -> () {
  45. println!("hello, this function '{}' has args but no return.", self.proc_name.as_str());
  46. println!("args data: {:?}", argsdata);
  47. }
  48. }
  49. zrfu_proc_def!{
  50. proc fn TestProcR () -> (retdata: TestProcCborOut) {
  51. println!("hello, this function '{}' has return but no args.", self.proc_name.as_str());
  52. let c_res = cbor!({
  53. "reimu" => "hakurei",
  54. "satori" => "komeiji",
  55. "koishi" => "komeiji",
  56. });
  57. let d_res = cbor!({
  58. "cirno" => 9,
  59. "satori" => 5,
  60. "remilia" => "scarlet",
  61. });
  62. let retdata = TestProcCborOut{
  63. a: String::from("hello, gensokyo"),
  64. b: 123456,
  65. c: c_res.unwrap(),
  66. d: d_res.unwrap(),
  67. };
  68. }
  69. }
  70. zrfu_proc_def!{
  71. proc fn TestProcN () -> () {
  72. println!("hello, this function '{}' has neither args nor return.", self.proc_name.as_str());
  73. }
  74. }
  75. /*
  76. proc_def_ar!(
  77. TestProcAR (TestProcCborIn as argsdata ; TestProcCborOut as retdata) {
  78. println!("hello, this function '{}' has args and return.", self.proc_name);
  79. println!("args data: {:?}", argsdata);
  80. let retdata = TestProcCborOut{
  81. a: argsdata.a.clone(),
  82. b: argsdata.b,
  83. c: argsdata.c.clone(),
  84. d: argsdata.d.clone(),
  85. };
  86. }
  87. );
  88. proc_def_a!(
  89. TestProcA (TestProcCborIn as argsdata) {
  90. println!("hello, this function '{}' has args but has no return.", self.proc_name);
  91. println!("args data: {:?}", argsdata);
  92. }
  93. );
  94. proc_def_r!(
  95. TestProcR (TestProcCborOut as retdata) {
  96. println!("hello, this function '{}' has no args but has return.", self.proc_name);
  97. let retdata = TestProcCborOut{
  98. a: String::from("hello, gensokyo"),
  99. b: 123456,
  100. c: cbor!({
  101. "reimu" => "hakurei",
  102. "satori" => "komeiji",
  103. "koishi" => "komeiji",
  104. }),
  105. d: cbor!({
  106. "cirno" => 9,
  107. "satori" => 5,
  108. "remilia" => "scarlet",
  109. }),
  110. };
  111. }
  112. );
  113. proc_def_n!(
  114. TestProcN () {
  115. println!("hello, this function '{}' has neither args nor return.", self.proc_name);
  116. }
  117. );
  118. */