dynamic-generics.rs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. use rustsbi::RustSBI;
  2. use sbi_spec::binary::SbiRet;
  3. // These structs should pass Rust build.
  4. #[derive(RustSBI)]
  5. #[rustsbi(dynamic)]
  6. struct WithGenerics<T: rustsbi::Timer> {
  7. reset: DummyReset,
  8. timer: T,
  9. info: DummyEnvInfo,
  10. }
  11. #[derive(RustSBI)]
  12. #[rustsbi(dynamic)]
  13. struct WithWhereClause<T>
  14. where
  15. T: rustsbi::Timer,
  16. {
  17. reset: DummyReset,
  18. timer: T,
  19. info: DummyEnvInfo,
  20. }
  21. #[derive(RustSBI)]
  22. #[rustsbi(dynamic)]
  23. struct WithConstantGenerics<const N: usize> {
  24. info: DummyEnvInfo,
  25. _dummy: [u8; N],
  26. }
  27. #[derive(RustSBI)]
  28. #[rustsbi(dynamic)]
  29. struct WithLifetime<'a> {
  30. info: &'a DummyEnvInfo,
  31. }
  32. #[derive(RustSBI)]
  33. #[rustsbi(dynamic)]
  34. struct WithEverythingCombined<'a, T: rustsbi::Timer, U, const N: usize>
  35. where
  36. U: rustsbi::Reset,
  37. {
  38. timer: T,
  39. reset: U,
  40. info: &'a DummyEnvInfo,
  41. _dummy: [u8; N],
  42. }
  43. #[test]
  44. fn test_impl_id() {
  45. let sbi = WithGenerics {
  46. reset: DummyReset,
  47. timer: DummyTimer,
  48. info: DummyEnvInfo,
  49. };
  50. assert_eq!(sbi.handle_ecall(0x10, 0x1, [0; 6]).value, 4);
  51. let sbi = WithWhereClause {
  52. reset: DummyReset,
  53. timer: DummyTimer,
  54. info: DummyEnvInfo,
  55. };
  56. assert_eq!(sbi.handle_ecall(0x10, 0x1, [0; 6]).value, 4);
  57. let sbi = WithConstantGenerics {
  58. info: DummyEnvInfo,
  59. _dummy: [0; 100],
  60. };
  61. assert_eq!(sbi.handle_ecall(0x10, 0x1, [0; 6]).value, 4);
  62. let dummy_info = DummyEnvInfo;
  63. let sbi = WithLifetime { info: &dummy_info };
  64. assert_eq!(sbi.handle_ecall(0x10, 0x1, [0; 6]).value, 4);
  65. let sbi = WithEverythingCombined {
  66. timer: DummyTimer,
  67. reset: DummyReset,
  68. info: &dummy_info,
  69. _dummy: [0; 10],
  70. };
  71. assert_eq!(sbi.handle_ecall(0x10, 0x1, [0; 6]).value, 4);
  72. }
  73. struct DummyReset;
  74. impl rustsbi::Reset for DummyReset {
  75. fn system_reset(&self, _: u32, _: u32) -> SbiRet {
  76. unimplemented!()
  77. }
  78. }
  79. struct DummyTimer;
  80. impl rustsbi::Timer for DummyTimer {
  81. fn set_timer(&self, _: u64) {
  82. unimplemented!()
  83. }
  84. }
  85. struct DummyEnvInfo;
  86. impl rustsbi::EnvInfo for DummyEnvInfo {
  87. fn mvendorid(&self) -> usize {
  88. unimplemented!()
  89. }
  90. fn marchid(&self) -> usize {
  91. unimplemented!()
  92. }
  93. fn mimpid(&self) -> usize {
  94. unimplemented!()
  95. }
  96. }