Skip to content

Instantly share code, notes, and snippets.

@scalexm
Created October 4, 2018 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scalexm/cf32cea831a5d8193fa8a484099b71a2 to your computer and use it in GitHub Desktop.
Save scalexm/cf32cea831a5d8193fa8a484099b71a2 to your computer and use it in GitHub Desktop.
Why functions shouldn't assume that their return type is WF
struct OnlySized<T: Sized>(std::marker::PhantomData<T>);
impl<T: Sized> OnlySized<T> {
fn new() -> Self {
println!("{}", std::mem::size_of::<T>());
OnlySized(std::marker::PhantomData)
}
}
trait Bar { }
fn foo() -> OnlySized<dyn Bar> {
OnlySized<dyn Bar>::new()
}
fn bar<T>(f: fn() -> T) {
let _ = f();
}
fn main() {
bar(foo);
}
// Comparison:
// defining a function `fn foo() -> OnlySized<dyn Bar>` is like defining a new type and then
// ```
// impl Fn<()> for foo {
// // here we should be proving that the assoc type value is WF, which is not the case
// type Output = OnlySized<dyn Bar>;
// }
// ```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment