Traits
Of course traits can also be generic. Here we define one which reimplements
the Drop trait as a generic method to drop itself and an input.
1. // Non-copyable types.
2. struct Empty;
3. struct Null;
5. // A trait generic over `T`.
6. trait DoubleDrop<T> {
7. // Define a method on the caller type which takes an
8. // additional single parameter `T` and does nothing with it.
9. fn double_drop(self, _: T);
10. }
12. // Implement `DoubleDrop<T>` for any generic parameter `T` and
13. // caller `U`.
14. impl<T, U> DoubleDrop<T> for U {
15. // This method takes ownership of both passed arguments,
16. // deallocating both.
17. fn double_drop(self, _: T) {}
18. }
20. fn main() {
21. let empty = Empty;
22. let null = Null;
24. // Deallocate `empty` and `null`.
25. empty.double_drop(null);
27. //empty;
28. //null;
29. // ^ TODO: Try uncommenting these lines.
30. }
