Skip to content

Instantly share code, notes, and snippets.

@MartinKavik
Created June 24, 2020 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MartinKavik/82503487178dfe8470ee50dec832d530 to your computer and use it in GitHub Desktop.
Save MartinKavik/82503487178dfe8470ee50dec832d530 to your computer and use it in GitHub Desktop.
S. + Style
trait StyleTrait: Sized {
fn font_size(self, font_size: u32) -> Style {
Style::default().font_size(font_size)
}
fn height(self, height: u32) -> Style {
Style::default().height(height)
}
}
struct S;
impl StyleTrait for S {}
#[derive(Default, Debug)]
struct Style {
font_size: u32,
height: u32,
}
impl StyleTrait for Style {
fn font_size(mut self, font_size: u32) -> Style {
self.font_size = font_size;
self
}
fn height(mut self, height: u32) -> Style {
self.height = height;
self
}
}
fn main() {
let style = S.font_size(16).height(50);
dbg!(style);
}
@MartinKavik
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment