Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Comments

@viruscamp
Copy link

viruscamp commented Oct 29, 2022

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;
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`
    //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`
/// }
/// ```
type Link<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`
type Link<T> = *mut Node<T>;
@viruscamp 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 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant