generic_enum.expanded.rs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. use tsify::Tsify;
  2. #[tsify(into_wasm_abi, from_wasm_abi)]
  3. pub enum GenericEnum<T, U> {
  4. Unit,
  5. NewType(T),
  6. Seq(T, U),
  7. Map { x: T, y: U },
  8. }
  9. #[automatically_derived]
  10. const _: () = {
  11. extern crate serde as _serde;
  12. use tsify::Tsify;
  13. use wasm_bindgen::{
  14. convert::{FromWasmAbi, IntoWasmAbi, OptionFromWasmAbi, OptionIntoWasmAbi},
  15. describe::WasmDescribe, prelude::*,
  16. };
  17. #[wasm_bindgen]
  18. extern "C" {
  19. #[wasm_bindgen(typescript_type = "GenericEnum")]
  20. pub type JsType;
  21. }
  22. impl<T, U> Tsify for GenericEnum<T, U> {
  23. type JsType = JsType;
  24. const DECL: &'static str = "export type GenericEnum<T, U> = \"Unit\" | { NewType: T } | { Seq: [T, U] } | { Map: { x: T; y: U } };";
  25. }
  26. #[wasm_bindgen(typescript_custom_section)]
  27. const TS_APPEND_CONTENT: &'static str = "export type GenericEnum<T, U> = \"Unit\" | { NewType: T } | { Seq: [T, U] } | { Map: { x: T; y: U } };";
  28. impl<T, U> WasmDescribe for GenericEnum<T, U> {
  29. #[inline]
  30. fn describe() {
  31. <Self as Tsify>::JsType::describe()
  32. }
  33. }
  34. impl<T, U> IntoWasmAbi for GenericEnum<T, U>
  35. where
  36. Self: _serde::Serialize,
  37. {
  38. type Abi = <JsType as IntoWasmAbi>::Abi;
  39. #[inline]
  40. fn into_abi(self) -> Self::Abi {
  41. self.into_js().unwrap_throw().into_abi()
  42. }
  43. }
  44. impl<T, U> OptionIntoWasmAbi for GenericEnum<T, U>
  45. where
  46. Self: _serde::Serialize,
  47. {
  48. #[inline]
  49. fn none() -> Self::Abi {
  50. <JsType as OptionIntoWasmAbi>::none()
  51. }
  52. }
  53. impl<T, U> FromWasmAbi for GenericEnum<T, U>
  54. where
  55. Self: _serde::de::DeserializeOwned,
  56. {
  57. type Abi = <JsType as FromWasmAbi>::Abi;
  58. #[inline]
  59. unsafe fn from_abi(js: Self::Abi) -> Self {
  60. let result = Self::from_js(&JsType::from_abi(js));
  61. if let Err(err) = result {
  62. wasm_bindgen::throw_str(err.to_string().as_ref());
  63. }
  64. result.unwrap_throw()
  65. }
  66. }
  67. impl<T, U> OptionFromWasmAbi for GenericEnum<T, U>
  68. where
  69. Self: _serde::de::DeserializeOwned,
  70. {
  71. #[inline]
  72. fn is_none(js: &Self::Abi) -> bool {
  73. <JsType as OptionFromWasmAbi>::is_none(js)
  74. }
  75. }
  76. };