You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In [7.2 Variance and Subtyping], we can add a simple function to prove that LinkedList<T> is covariant over T, and type Link<T> = *mut Node<T> is not like so
#259
Open
viruscamp opened this issue
Oct 29, 2022
· 0 comments
In 7.2 Variance and Subtyping , we can add a simple function to prove that LinkedList<T> is covariant over T.
use too_many_linked_list::unsafe_deque::LinkedList;fnensure_covariant<'long:'short,'short>(list_long:LinkedList<&'long i32>,mutlist_short:LinkedList<&'short i32>){let list_short_new:LinkedList<&'short i32> = list_long;// to prove `LinkedList<T>` is covariant over `T`//let list_long_new: LinkedList<&'long i32> = list_short; // to prove `LinkedList<T>` is contravariant over `T`}
/// We can prove that `LinkedList<T>` is covariant over `T`./// ```no_run/// # use too_many_linked_list::unsafe_deque::LinkedList;/// fn ensure_covariant<'long: 'short, 'short>(list_long: LinkedList<&'long i32>, mut list_short: LinkedList<&'short i32>) {/// let list_short_new: LinkedList<&'short i32> = list_long; // to prove `LinkedList<T>` is covariant over `T`/// }/// ```typeLink<T> = Option<NonNull<Node<T>>>;
/// We cannot prove that `LinkedList<T>` is covariant over `T`, if we use `type Link<T> = *mut Node<T>;`/// ```compile_fail/// # use too_many_linked_list::unsafe_deque::ptr_mut::LinkedList;/// fn ensure_covariant<'long: 'short, 'short>(list_long: LinkedList<&'long i32>, mut list_short: LinkedList<&'short i32>) {/// let list_short_new: LinkedList<&'short i32> = list_long; // to prove `LinkedList<T>` is covariant over `T`/// }/// ```/// compiler errors:/// = note: requirement occurs because of the type `ptr_mut::LinkedList<&i32>`, which makes the generic argument `&i32` invariant/// = note: the struct `ptr_mut::LinkedList<T>` is invariant over the parameter `T`typeLink<T> = *mutNode<T>;
The text was updated successfully, but these errors were encountered:
viruscamp
changed the title
We can add a simple function to prove that LinkedList<T> is covariant over T, and type Link<T> = *mut Node<T> is not like so
In [7.2 Variance and Subtyping](https://rust-unofficial.github.io/too-many-lists/sixth-variance.html), we can add a simple function to prove that LinkedList<T> is covariant over T, and type Link<T> = *mut Node<T> is not like so
Oct 29, 2022
viruscamp
changed the title
In [7.2 Variance and Subtyping](https://rust-unofficial.github.io/too-many-lists/sixth-variance.html), we can add a simple function to prove that LinkedList<T> is covariant over T, and type Link<T> = *mut Node<T> is not like so
In [7.2 Variance and Subtyping], we can add a simple function to prove that LinkedList<T> is covariant over T, and type Link<T> = *mut Node<T> is not like so
Oct 29, 2022
In 7.2 Variance and Subtyping , we can add a simple function to prove that
LinkedList<T>
is covariant overT
.The text was updated successfully, but these errors were encountered: