From b0ca76b3d5e7142fa645b5a4fa300c0870f469a4 Mon Sep 17 00:00:00 2001 From: oblique Date: Sun, 9 Aug 2020 21:37:19 +0300 Subject: [PATCH] waker context: Fix `size` and `align` of raw vtable --- src/2_waker_context.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/2_waker_context.md b/src/2_waker_context.md index bc0741b..d7ec878 100644 --- a/src/2_waker_context.md +++ b/src/2_waker_context.md @@ -98,6 +98,7 @@ Let's explain this in code instead of words by implementing our own trait object from these parts: ```rust +# use std::mem::{align_of, size_of}; // A reference to a trait object is a fat pointer: (data_ptr, vtable_ptr) trait Test { fn add(&self) -> i32; @@ -138,9 +139,9 @@ fn main() { // format where the three first values has a special meaning like the // length of the array is encoded in the array itself as the second value. let vtable = vec![ - 0, // pointer to `Drop` (which we're not implementing here) - 6, // length of vtable - 8, // alignment + 0, // pointer to `Drop` (which we're not implementing here) + size_of::(), // length of data + align_of::(), // alignment of data // we need to make sure we add these in the same order as defined in the Trait. add as usize, // function pointer - try changing the order of `add`