You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
add to_owned() methods to Proplist and format::Info
this has proven necessary for adding a lifetime annotation to the
`Proplist` struct and consequently also to `format::Info`. (to follow if
worthwhile).
previously the `to_owned()` methods on introspection structs were creating
owned copies of these two types via their `Clone` implementations (which
always return owned copies). when experimenting with adding the mentioned
lifetime annotations (including changing the output of the `Clone`
implementations to `Foo<'static>`), i ran into confusing compiler errors
complaining that the lifetime of the objects created (in the introspection
`to_owned()` methods) might not live for the `'static` output lifetime.
there was no error to suggest that my modified `Clone` implementations were
invalid, so i couldn't understand what the problem was. i ended up creating
`to_owned()` methods on the `Proplist` and `format::Info` structs,
containing identical code to their `Clone` implementations, and tried using
these methods instead. this fixed the errors. so it seems that for some
reason the `Clone` implementations were not actually honouring the`'static`
output lifetime I'd expressed.
why? the `Clone` trait itself has no lifetime constraints, and does not
provide an associated type for specifying the output of `clone()` which
explicitly specifies `Self` as the output. so either you're not actually
allowed to override the `Self` output like I did, in which case the compiler
let me down by not pointing to the true source of the error; or the compiler
mistakenly derived the lifetime from the `Self` expressed in the trait,
rather the output expression in my implementation. interestingly (since
lifetimes are somewhat poorly explained in rust documentation), from
experimentation i've discovered that `Self` does not take lifetime(s) from
function inputs, but rather from the `impl` line. thus in my commit where
I'd added the lifetime to `Proplist`, if the compiler was indeed using the
`Self` output expressed in the trait definition, then that `Self` would
have taken the lifetime associated with the `Proplist` instance refered to
in `&self`, which would then make sense why the lifetime would not have
been long enough. but still, the compiler either has a bug picking the
correct lifetime, or a deficiency in pointing out the true source of the
error.
additionally, the fact that my `Clone` implementations for these structs
always output owned instances is not necessarily inline with proper
expectations of cloning. note that `std::borrow::Cow` can hold both
borrowed and owned types, and its clone behaviour is such that it creates a
duplicate that matches the original, thus borrowed if the original is
borrowed, or owned if the original is owned. mine should probably have the
same behaviour. if i do change mine accordingly, I'd need such `to_owned()`
methods as added here to provide a means of forcing the creation of an
owned instance from a borrowed one.
note that the return type of `Self` here is fine since the types do not
currently have a lifetime annotation, so effectively the return instances
are owned/static.
0 commit comments