|
| 1 | +#![feature(dyn_star)] |
| 2 | +#![allow(incomplete_features)] |
| 3 | + |
| 4 | +use std::fmt::{Debug, Display}; |
| 5 | + |
| 6 | +fn main() { |
| 7 | + make_dyn_star(); |
| 8 | + method(); |
| 9 | + box_(); |
| 10 | + dispatch_on_pin_mut(); |
| 11 | + dyn_star_to_dyn(); |
| 12 | + dyn_to_dyn_star(); |
| 13 | +} |
| 14 | + |
| 15 | +fn dyn_star_to_dyn() { |
| 16 | + let x: dyn* Debug = &42; |
| 17 | + let x = Box::new(x) as Box<dyn Debug>; |
| 18 | + assert_eq!("42", format!("{x:?}")); |
| 19 | +} |
| 20 | + |
| 21 | +fn dyn_to_dyn_star() { |
| 22 | + let x: Box<dyn Debug> = Box::new(42); |
| 23 | + let x = &x as dyn* Debug; |
| 24 | + assert_eq!("42", format!("{x:?}")); |
| 25 | +} |
| 26 | + |
| 27 | +fn make_dyn_star() { |
| 28 | + fn make_dyn_star_coercion(i: usize) { |
| 29 | + let _dyn_i: dyn* Debug = i; |
| 30 | + } |
| 31 | + |
| 32 | + fn make_dyn_star_explicit(i: usize) { |
| 33 | + let _dyn_i: dyn* Debug = i as dyn* Debug; |
| 34 | + } |
| 35 | + |
| 36 | + make_dyn_star_coercion(42); |
| 37 | + make_dyn_star_explicit(42); |
| 38 | +} |
| 39 | + |
| 40 | +fn method() { |
| 41 | + trait Foo { |
| 42 | + fn get(&self) -> usize; |
| 43 | + } |
| 44 | + |
| 45 | + impl Foo for usize { |
| 46 | + fn get(&self) -> usize { |
| 47 | + *self |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + fn invoke_dyn_star(i: dyn* Foo) -> usize { |
| 52 | + i.get() |
| 53 | + } |
| 54 | + |
| 55 | + fn make_and_invoke_dyn_star(i: usize) -> usize { |
| 56 | + let dyn_i: dyn* Foo = i; |
| 57 | + invoke_dyn_star(dyn_i) |
| 58 | + } |
| 59 | + |
| 60 | + assert_eq!(make_and_invoke_dyn_star(42), 42); |
| 61 | +} |
| 62 | + |
| 63 | +fn box_() { |
| 64 | + fn make_dyn_star() -> dyn* Display { |
| 65 | + Box::new(42) as dyn* Display |
| 66 | + } |
| 67 | + |
| 68 | + let x = make_dyn_star(); |
| 69 | + assert_eq!(format!("{x}"), "42"); |
| 70 | +} |
| 71 | + |
| 72 | +fn dispatch_on_pin_mut() { |
| 73 | + use std::future::Future; |
| 74 | + |
| 75 | + async fn foo(f: dyn* Future<Output = i32>) { |
| 76 | + println!("dispatch_on_pin_mut: value: {}", f.await); |
| 77 | + } |
| 78 | + |
| 79 | + async fn async_main() { |
| 80 | + foo(Box::pin(async { 1 })).await |
| 81 | + } |
| 82 | + |
| 83 | + // ------------------------------------------------------------------------- // |
| 84 | + // Implementation Details Below... |
| 85 | + |
| 86 | + use std::pin::Pin; |
| 87 | + use std::task::*; |
| 88 | + |
| 89 | + pub fn noop_waker() -> Waker { |
| 90 | + let raw = RawWaker::new(std::ptr::null(), &NOOP_WAKER_VTABLE); |
| 91 | + |
| 92 | + // SAFETY: the contracts for RawWaker and RawWakerVTable are upheld |
| 93 | + unsafe { Waker::from_raw(raw) } |
| 94 | + } |
| 95 | + |
| 96 | + const NOOP_WAKER_VTABLE: RawWakerVTable = RawWakerVTable::new(noop_clone, noop, noop, noop); |
| 97 | + |
| 98 | + unsafe fn noop_clone(_p: *const ()) -> RawWaker { |
| 99 | + RawWaker::new(std::ptr::null(), &NOOP_WAKER_VTABLE) |
| 100 | + } |
| 101 | + |
| 102 | + unsafe fn noop(_p: *const ()) {} |
| 103 | + |
| 104 | + let mut fut = async_main(); |
| 105 | + |
| 106 | + // Poll loop, just to test the future... |
| 107 | + let waker = noop_waker(); |
| 108 | + let ctx = &mut Context::from_waker(&waker); |
| 109 | + |
| 110 | + loop { |
| 111 | + match unsafe { Pin::new_unchecked(&mut fut).poll(ctx) } { |
| 112 | + Poll::Pending => {} |
| 113 | + Poll::Ready(()) => break, |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments