Have DateTimeWidget include the time zone in its type. #1
Loading…
x
Reference in New Issue
Block a user
No description provided.
Delete Branch "ggriffiniii/i3xs:master"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@ -35,0 +29,4 @@impl<Tz> DateTimeWidget<Tz>whereTz: TimeZone,Tz::Offset: Display,I don't get why "Tz::Offset: Display" is used here, can you explain that?
Well I think it may not actually be necessary here (and should probably be removed), but it is necessary on the Widget impl. Short answer for why it's necessary there is because the compiler said so. Longer answer is because the Widget impl formats the time, which requires displaying the timezone which can only be done if the timezone's offset has a Display impl.
@ -40,3 +40,2 @@}pub fn set_colors(&mut self, colors: &[TimeColor]) {let mut colors = colors.to_vec();pub fn set_colors(&mut self, colors: impl Into<Vec<TimeColor>>) {Now that I look at the docs, sort_by() is defined on slice. So this function only needs colors to be sort_by()'able, why make it something that can go into vec?
Is it because the thing eventually needs to be a Vec, so make the parameter Into?
I get a little confused what the better parameter is smart containers or references to base types. It seems like &str is better than String, if your function allows it, so I was thinking &[TimeColor] would be better than Vec. Thoughts?
Here are the benefits that I see, but I think there's plenty of room for reasonable people to disagree on these things.
Yes, it's because you eventually need a
Vecto store in your struct. Since that's the end result there are potential efficiency gains by accepting anything that can beInto<Vec<TimeColor>>rather than&[TimeColor]and additional flexibility for the caller.Consider the following ways to call the function if it accepts
&[TimeColor]:both of these will allocate a new vector within
set_colors. The second case is really unfortunate because the caller just allocated their own vec just for the purpose of passing it toset_colorsonly to haveset_colorsallocate it's own identical copy.Consider the following ways to call the function if it accepts
impl Into<Vec<TimeColor>>:The first 2 are identical and result in identical code where
set_colorsallocates a new vector. The 3rd is the new variant. This will take thevecthat's already created by the caller and pass it toset_colorsandset_colorswill simply sort it and assign it into the struct without needing to allocate a newvec.