generics.rs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. use std::fmt::Display;
  2. use dioxus::prelude::*;
  3. // This test just checks that props compile with generics
  4. // It will not actually run any code
  5. #[test]
  6. #[allow(unused)]
  7. #[allow(non_snake_case)]
  8. fn generic_props_compile() {
  9. fn app() -> Element {
  10. rsx! {
  11. TakesClone {
  12. value: "hello world"
  13. }
  14. TakesCloneManual {
  15. value: "hello world"
  16. }
  17. TakesCloneManualWhere {
  18. value: "hello world"
  19. }
  20. GenericFnWhereClause {
  21. value: "hello world"
  22. }
  23. }
  24. }
  25. #[component]
  26. fn TakesClone<T: Clone + PartialEq + 'static>(value: T) -> Element {
  27. rsx! {}
  28. }
  29. #[component]
  30. fn TakesCloneArc<T: PartialEq + 'static>(value: std::sync::Arc<T>) -> Element {
  31. rsx! {}
  32. }
  33. struct MyBox<T>(std::marker::PhantomData<T>);
  34. impl<T: Display> Clone for MyBox<T> {
  35. fn clone(&self) -> Self {
  36. MyBox(std::marker::PhantomData)
  37. }
  38. }
  39. impl<T: Display> PartialEq for MyBox<T> {
  40. fn eq(&self, _: &Self) -> bool {
  41. true
  42. }
  43. }
  44. #[component]
  45. #[allow(clippy::multiple_bound_locations)]
  46. fn TakesCloneMyBox<T: 'static>(value: MyBox<T>) -> Element
  47. where
  48. T: Display,
  49. {
  50. rsx! {}
  51. }
  52. #[derive(Props, Clone, PartialEq)]
  53. struct TakesCloneManualProps<T: Clone + PartialEq + 'static> {
  54. value: T,
  55. }
  56. fn TakesCloneManual<T: Clone + PartialEq>(props: TakesCloneManualProps<T>) -> Element {
  57. rsx! {}
  58. }
  59. #[derive(Props, Clone, PartialEq)]
  60. struct TakesCloneManualWhereProps<T>
  61. where
  62. T: Clone + PartialEq + 'static,
  63. {
  64. value: T,
  65. }
  66. fn TakesCloneManualWhere<T: Clone + PartialEq>(
  67. props: TakesCloneManualWhereProps<T>,
  68. ) -> Element {
  69. rsx! {}
  70. }
  71. #[derive(Props, Clone, PartialEq)]
  72. struct TakesCloneManualWhereWithOwnerProps<T>
  73. where
  74. T: Clone + PartialEq + 'static,
  75. {
  76. value: EventHandler<T>,
  77. }
  78. fn TakesCloneManualWhereWithOwner<T: Clone + PartialEq>(
  79. props: TakesCloneManualWhereWithOwnerProps<T>,
  80. ) -> Element {
  81. rsx! {}
  82. }
  83. #[component]
  84. fn GenericFnWhereClause<T>(value: T) -> Element
  85. where
  86. T: Clone + PartialEq + Display + 'static,
  87. {
  88. rsx! {
  89. p { "{value}" }
  90. }
  91. }
  92. }