Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. #![feature(never_type)]
  2. #![feature(specialization)]
  3.  
  4. use core::ops::Range;
  5.  
  6. //
  7. // Public
  8. //
  9.  
  10. // Utilities
  11. pub trait FamilyLt<'r> {
  12. type Out;
  13. }
  14.  
  15. pub struct IdFamily<T>(std::marker::PhantomData<T>, !);
  16. impl<'a, T> FamilyLt<'a> for IdFamily<T> {
  17. type Out = T;
  18. }
  19.  
  20. pub struct SlicesFamily<T>(std::marker::PhantomData<T>, !);
  21. impl<'a, T00: 'static> FamilyLt<'a> for SlicesFamily<(T00, )> {
  22. type Out = (&'a [T00], );
  23. }
  24. impl<'a, T00: 'static, T01: 'static> FamilyLt<'a> for SlicesFamily<(T00, T01)> {
  25. type Out = (&'a [T00], &'a [T01]);
  26. }
  27.  
  28. pub type SlicesOf<'a, T> = <<T as Tuple>::Slices as FamilyLt<'a>>::Out;
  29. pub trait Tuple {
  30. type Slices: for<'r> FamilyLt<'r>;
  31. }
  32.  
  33. // Tuple impls
  34. impl Tuple for () {
  35. type Slices = IdFamily<()>;
  36. }
  37.  
  38. impl<T00: 'static> Tuple for (T00, ) {
  39. type Slices = SlicesFamily<(T00, )>;
  40. }
  41.  
  42. impl<T00: 'static, T01: 'static> Tuple for (T00, T01) {
  43. type Slices = SlicesFamily<(T00, T01)>;
  44. }
  45.  
  46. // Slices
  47. pub struct Slices<'a, T: Tuple> {
  48. inner: Inner<'a, T>
  49. }
  50.  
  51. impl<'a, T: Tuple> Slices<'a, T> {
  52. pub fn new(slices: SlicesOf<'a, T>) -> Self {
  53. Self {
  54. inner: Inner {
  55. slices
  56. }
  57. }
  58. }
  59. }
  60.  
  61. //
  62. // Internal
  63. //
  64.  
  65. struct Inner<'a, T: Tuple> {
  66. slices: SlicesOf<'a, T>
  67. }
  68.  
  69. trait InnerSpecialized {
  70. fn index(&self, range: Range<usize>) -> Self;
  71. fn drop(&mut self);
  72. }
  73.  
  74. impl<'a, T: Tuple> InnerSpecialized for Inner<'a, T> {
  75. default fn index(&self, range: Range<usize>) -> Self { panic!("Not implemented for this type.") }
  76. default fn drop(&mut self) { panic!("Not implemented for this type.") }
  77. }
  78.  
  79. impl<'a, T: Tuple> Drop for Inner<'a, T> {
  80. fn drop(&mut self) {
  81. <Self as InnerSpecialized>::drop(self);
  82. }
  83. }
  84.  
  85. //
  86. // Specializations
  87. //
  88.  
  89. impl<'a, T00: 'static> InnerSpecialized for Inner<'a, (T00, )> {
  90. fn index(&self, range: Range<usize>) -> Self {
  91. Self {
  92. slices: (&self.slices.0[range], )
  93. }
  94. }
  95. fn drop(&mut self) {
  96. }
  97. }
  98.  
  99. //
  100. // Usage
  101. //
  102.  
  103. fn main() {
  104. let v0: &[usize] = &[1, 2, 3, 4];
  105. let v1: &[bool] = &[true, false, true, true];
  106.  
  107. let slices: Slices<(usize, )> = Slices::new((v0, ));
  108. drop(slices); // Works (specialization is available)
  109.  
  110. let slices: Slices<(usize, bool)> = Slices::new((v0, v1));
  111. drop(slices); // Fails (default impl)
  112.  
  113. println!("Hello, world!");
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement