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

Missing lifetime when doing from_ref #16

Closed
vnghia opened this issue Aug 26, 2024 · 4 comments · Fixed by #19
Closed

Missing lifetime when doing from_ref #16

vnghia opened this issue Aug 26, 2024 · 4 comments · Fixed by #19

Comments

@vnghia
Copy link
Contributor

vnghia commented Aug 26, 2024

Hi thank you for this wonderful crate. One thing I notice is this does not work well with type with lifetime. For example this code does not work.

pub struct Source {
	pub name: String,
}

#[derive(o2o)]
pub struct Destination<'a> {
	#[from((&~).into())]
	pub name: Cow<'a, str>
}

// Generated code
impl<'a> ::core::convert::From<&Source> for Destination<'a> {
    fn from(value: &Source) -> Destination<'a> {
        Data {
            name: (&value.name).into(),
        }
    }
}
Repository owner deleted a comment Aug 26, 2024
@Artem-Romanenia
Copy link
Owner

Hi, thank you for reporting this! Will be looking into it, hopefully sooner than later.

@vnghia
Copy link
Contributor Author

vnghia commented Aug 31, 2024

Hey @Artem-Romanenia, I think we have to discuss about the syntax here. The syntax Struct::<T> seems to be not standard and can not be parsed by syn. Can we switch to Struct<T> ?

I still can not wrap my head around how to distinguish between Struct<T> where T is a generic and Struct<f32> where this is a concrete type. If that is too hard, I will probably just add lifetime generic instead of all generic.

@Artem-Romanenia
Copy link
Owner

Hi @vnghia, I'm not quite sure what you mean by this

I think we have to discuss about the syntax here. The syntax Struct::<T> seems to be not standard and can not be parsed by syn. Can we switch to Struct<T> ?

I suspect you are dealing with the following scenario

struct Entity<T> {
    field: T,
}

#[derive(o2o::o2o)]
#[map(Entity::<i16>)]
struct EntityDto {
    field: i16,
}

In this case, I think o2o doesn't care how it is spelled, it can be written as

struct Entity<T> {
    field: T,
}

#[derive(o2o::o2o)]
#[map(Entity<i16>)] // <- No turbofish here
struct EntityDto {
    field: i16,
}

and they will both expand in the same code. Honestly, I'm quite confused by rusts's turbofish in the first place 😄, but they just both happen to work.

I still can not wrap my head around how to distinguish between Struct where T is a generic and Struct where this is a concrete type. If that is too hard, I will probably just add lifetime generic instead of all generic.

So, previously, I used to think that mapping regular struct EntityDto to open generic struct Entity<T> didn't make any sense, but I've just managed to come up with this example:

struct Entity<T> {
    field: T,
}

struct EntityDto {
    field: i16,
}

fn test<T>(t: Entity<T>) -> i16 {
    // Do something with t
    42
}

impl<T> From<Entity<T>> for EntityDto {
    fn from(value: Entity<T>) -> Self {
        EntityDto {
            field: test(value)
        }
    }
}

Now I think it makes some sense, but it is still hard to imagine some poor soul ever wanting to do this.

So, I guess, for now let o2o bother only with mapping between regular EntityDto and closed generic Entity<i32> (which I think is already supported).

If anybody ever needs to map EntityDto and Entity<T>, and they want it hard enough to bother to create an issue on github, I'll think of some way to support this... Perhaps it would be a syntax like

#[derive(o2o::o2o)]
#[from_owned(Entity<T>| generic_impl_params(T))]
struct EntityDto {
    #[from(test(@))]
    field: i16,
}

@Artem-Romanenia
Copy link
Owner

Hi! New behavior regarding lifetimes is available in v.0.4.10.

Thanks again for the issue and for the pr!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants