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

Yokeable Derive: require Sized on certain types #4659

Merged
merged 2 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion utils/yoke/derive/examples/yoke_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#![allow(unused)]

use std::borrow::Cow;
use std::{borrow::Cow, marker::PhantomData};
use yoke::{Yoke, Yokeable};
use zerovec::{maps::ZeroMapKV, ule::AsULE, VarZeroVec, ZeroMap, ZeroVec};

Expand Down Expand Up @@ -67,6 +67,25 @@ pub struct ZeroMapGenericExample<'a, T: for<'b> ZeroMapKV<'b> + ?Sized> {
map: ZeroMap<'a, str, T>,
}

#[derive(Yokeable)]
pub struct MaybeSizedWrap<T, Q: ?Sized, U: ?Sized> {
x: T,
y: Option<T>,
ignored: PhantomData<U>,
q: Q,
}

// TODO(#4119): Make this example compile
/*
#[derive(Yokeable)]
pub struct MaybeSizedWrapWithLifetime<'a, T, Q: ?Sized, U: ?Sized> {
x: T,
y: Option<T>,
ignored: &'a U,
q: Q,
}
*/

pub struct AssertYokeable {
string: Yoke<StringExample, Box<[u8]>>,
int: Yoke<IntExample, Box<[u8]>>,
Expand All @@ -81,6 +100,9 @@ pub struct AssertYokeable {
map: Yoke<ZeroMapExample<'static>, Box<[u8]>>,
map_gen1: Yoke<ZeroMapGenericExample<'static, u32>, Box<[u8]>>,
map_gen2: Yoke<ZeroMapGenericExample<'static, str>, Box<[u8]>>,
maybe_sized_wrap: Yoke<MaybeSizedWrap<usize, usize, str>, Box<[u8]>>,
// TODO(#4119): Make this example compile
// maybe_sized_wrap_with_lt: Yoke<MaybeSizedWrapWithLifetime<'static, usize, usize, str>, Box<[u8]>>,
}

fn main() {}
4 changes: 2 additions & 2 deletions utils/yoke/derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fn yokeable_derive_impl(input: &DeriveInput) -> TokenStream2 {
let name = &input.ident;
quote! {
// This is safe because there are no lifetime parameters.
unsafe impl<'a, #(#tybounds),*> yoke::Yokeable<'a> for #name<#(#typarams),*> where #(#static_bounds),* {
unsafe impl<'a, #(#tybounds),*> yoke::Yokeable<'a> for #name<#(#typarams),*> where #(#static_bounds,)* Self: Sized {
type Output = Self;
#[inline]
fn transform(&self) -> &Self::Output {
Expand Down Expand Up @@ -220,7 +220,7 @@ fn yokeable_derive_impl(input: &DeriveInput) -> TokenStream2 {
//
// This custom derive can be improved to handle this case when
// necessary
unsafe impl<'a, #(#tybounds),*> yoke::Yokeable<'a> for #name<'static, #(#typarams),*> where #(#static_bounds),* {
unsafe impl<'a, #(#tybounds),*> yoke::Yokeable<'a> for #name<'static, #(#typarams),*> where #(#static_bounds,)* {
type Output = #name<'a, #(#typarams),*>;
#[inline]
fn transform(&'a self) -> &'a Self::Output {
Expand Down
Loading