Hack to specialize `W: Write` for `Vec<u8>`?

Great! I forgot about that trick. Here's the implementation:

pub(crate) fn is_vec_u8<T>() -> bool {
    use std::cell::Cell;
    use std::marker::PhantomData;

    struct IsVecU8<'a, T>(&'a Cell<bool>, PhantomData<T>);
    // Rust specializes Copy and doesn't call Clone
    impl Copy for IsVecU8<'_, Vec<u8>> {}

    impl<T> Clone for IsVecU8<'_, T> {
        fn clone(&self) -> Self {
            self.0.set(false);
            Self(self.0, self.1)
        }
    }
    let cell = Cell::new(true);
    let _x = [IsVecU8::<'_, T>(&cell, PhantomData)].clone();
    if cell.get() {
        assert_eq!(std::mem::size_of::<T>(), std::mem::size_of::<Vec<u8>>());
        assert_eq!(std::mem::align_of::<T>(), std::mem::align_of::<Vec<u8>>());
        true
    } else {
        false
    }
}

#[test]
fn spec() {
    assert!(!is_vec_u8::<bool>());
    assert!(!is_vec_u8::<f32>());
    assert!(!is_vec_u8::<String>());
    assert!(!is_vec_u8::<Vec<i32>>());
    assert!(is_vec_u8::<Vec<u8>>());
}
6 Likes