diff options
author | 2019-11-29 21:24:52 -0500 | |
---|---|---|
committer | 2019-11-29 21:24:52 -0500 | |
commit | 267e242238fab0aba14fb4c2e27269ce3a3e3951 (patch) | |
tree | 6957be383f221ecc444ac50cdcbdfd45f6374349 /native/src | |
parent | 811d8b90d71c26100f0933217f5474e090fbf17c (diff) | |
download | iced-267e242238fab0aba14fb4c2e27269ce3a3e3951.tar.gz iced-267e242238fab0aba14fb4c2e27269ce3a3e3951.tar.bz2 iced-267e242238fab0aba14fb4c2e27269ce3a3e3951.zip |
Make many functions `const`
The point is to set up repeated components or boilerplate before their
use sites.
The majority of these make sense as `const`. However, some functions
such as those regarding state may not make sense as `const`.
Diffstat (limited to 'native/src')
-rw-r--r-- | native/src/layout/limits.rs | 6 | ||||
-rw-r--r-- | native/src/layout/node.rs | 8 | ||||
-rw-r--r-- | native/src/widget/column.rs | 16 | ||||
-rw-r--r-- | native/src/widget/image.rs | 4 | ||||
-rw-r--r-- | native/src/widget/row.rs | 16 | ||||
-rw-r--r-- | native/src/widget/slider.rs | 4 | ||||
-rw-r--r-- | native/src/widget/text_input.rs | 11 |
7 files changed, 34 insertions, 31 deletions
diff --git a/native/src/layout/limits.rs b/native/src/layout/limits.rs index 2705a47d..5f456871 100644 --- a/native/src/layout/limits.rs +++ b/native/src/layout/limits.rs @@ -20,7 +20,7 @@ impl Limits { /// /// [`Limits`]: struct.Limits.html /// [`Size`]: ../struct.Size.html - pub fn new(min: Size, max: Size) -> Limits { + pub const fn new(min: Size, max: Size) -> Limits { Limits { min, max, @@ -32,7 +32,7 @@ impl Limits { /// /// [`Limits`]: struct.Limits.html /// [`Size`]: ../struct.Size.html - pub fn min(&self) -> Size { + pub const fn min(&self) -> Size { self.min } @@ -40,7 +40,7 @@ impl Limits { /// /// [`Limits`]: struct.Limits.html /// [`Size`]: ../struct.Size.html - pub fn max(&self) -> Size { + pub const fn max(&self) -> Size { self.max } diff --git a/native/src/layout/node.rs b/native/src/layout/node.rs index ed1cd3da..3b63914e 100644 --- a/native/src/layout/node.rs +++ b/native/src/layout/node.rs @@ -12,7 +12,7 @@ impl Node { /// /// [`Node`]: struct.Node.html /// [`Size`]: ../struct.Size.html - pub fn new(size: Size) -> Self { + pub const fn new(size: Size) -> Self { Self::with_children(size, Vec::new()) } @@ -20,7 +20,7 @@ impl Node { /// /// [`Node`]: struct.Node.html /// [`Size`]: ../struct.Size.html - pub fn with_children(size: Size, children: Vec<Node>) -> Self { + pub const fn with_children(size: Size, children: Vec<Node>) -> Self { Node { bounds: Rectangle { x: 0.0, @@ -36,14 +36,14 @@ impl Node { /// /// [`Node`]: struct.Node.html /// [`Size`]: ../struct.Size.html - pub fn size(&self) -> Size { + pub const fn size(&self) -> Size { Size::new(self.bounds.width, self.bounds.height) } /// Returns the bounds of the [`Node`]. /// /// [`Node`]: struct.Node.html - pub fn bounds(&self) -> Rectangle { + pub const fn bounds(&self) -> Rectangle { self.bounds } diff --git a/native/src/widget/column.rs b/native/src/widget/column.rs index cdcf25af..104fdb94 100644 --- a/native/src/widget/column.rs +++ b/native/src/widget/column.rs @@ -28,7 +28,7 @@ impl<'a, Message, Renderer> Column<'a, Message, Renderer> { /// Creates an empty [`Column`]. /// /// [`Column`]: struct.Column.html - pub fn new() -> Self { + pub const fn new() -> Self { Column { spacing: 0, padding: 0, @@ -46,7 +46,7 @@ impl<'a, Message, Renderer> Column<'a, Message, Renderer> { /// Custom margins per element do not exist in Iced. You should use this /// method instead! While less flexible, it helps you keep spacing between /// elements consistent. - pub fn spacing(mut self, units: u16) -> Self { + pub const fn spacing(mut self, units: u16) -> Self { self.spacing = units; self } @@ -54,7 +54,7 @@ impl<'a, Message, Renderer> Column<'a, Message, Renderer> { /// Sets the padding of the [`Column`]. /// /// [`Column`]: struct.Column.html - pub fn padding(mut self, units: u16) -> Self { + pub const fn padding(mut self, units: u16) -> Self { self.padding = units; self } @@ -62,7 +62,7 @@ impl<'a, Message, Renderer> Column<'a, Message, Renderer> { /// Sets the width of the [`Column`]. /// /// [`Column`]: struct.Column.html - pub fn width(mut self, width: Length) -> Self { + pub const fn width(mut self, width: Length) -> Self { self.width = width; self } @@ -70,7 +70,7 @@ impl<'a, Message, Renderer> Column<'a, Message, Renderer> { /// Sets the height of the [`Column`]. /// /// [`Column`]: struct.Column.html - pub fn height(mut self, height: Length) -> Self { + pub const fn height(mut self, height: Length) -> Self { self.height = height; self } @@ -78,7 +78,7 @@ impl<'a, Message, Renderer> Column<'a, Message, Renderer> { /// Sets the maximum width of the [`Column`]. /// /// [`Column`]: struct.Column.html - pub fn max_width(mut self, max_width: u32) -> Self { + pub const fn max_width(mut self, max_width: u32) -> Self { self.max_width = max_width; self } @@ -86,7 +86,7 @@ impl<'a, Message, Renderer> Column<'a, Message, Renderer> { /// Sets the maximum height of the [`Column`] in pixels. /// /// [`Column`]: struct.Column.html - pub fn max_height(mut self, max_height: u32) -> Self { + pub const fn max_height(mut self, max_height: u32) -> Self { self.max_height = max_height; self } @@ -94,7 +94,7 @@ impl<'a, Message, Renderer> Column<'a, Message, Renderer> { /// Sets the horizontal alignment of the contents of the [`Column`] . /// /// [`Column`]: struct.Column.html - pub fn align_items(mut self, align: Align) -> Self { + pub const fn align_items(mut self, align: Align) -> Self { self.align_items = align; self } diff --git a/native/src/widget/image.rs b/native/src/widget/image.rs index 4c588c9d..5cfe074f 100644 --- a/native/src/widget/image.rs +++ b/native/src/widget/image.rs @@ -37,7 +37,7 @@ impl Image { /// Sets the width of the [`Image`] boundaries. /// /// [`Image`]: struct.Image.html - pub fn width(mut self, width: Length) -> Self { + pub const fn width(mut self, width: Length) -> Self { self.width = width; self } @@ -45,7 +45,7 @@ impl Image { /// Sets the height of the [`Image`] boundaries. /// /// [`Image`]: struct.Image.html - pub fn height(mut self, height: Length) -> Self { + pub const fn height(mut self, height: Length) -> Self { self.height = height; self } diff --git a/native/src/widget/row.rs b/native/src/widget/row.rs index c854aff7..e9b8654c 100644 --- a/native/src/widget/row.rs +++ b/native/src/widget/row.rs @@ -28,7 +28,7 @@ impl<'a, Message, Renderer> Row<'a, Message, Renderer> { /// Creates an empty [`Row`]. /// /// [`Row`]: struct.Row.html - pub fn new() -> Self { + pub const fn new() -> Self { Row { spacing: 0, padding: 0, @@ -46,7 +46,7 @@ impl<'a, Message, Renderer> Row<'a, Message, Renderer> { /// Custom margins per element do not exist in Iced. You should use this /// method instead! While less flexible, it helps you keep spacing between /// elements consistent. - pub fn spacing(mut self, units: u16) -> Self { + pub const fn spacing(mut self, units: u16) -> Self { self.spacing = units; self } @@ -54,7 +54,7 @@ impl<'a, Message, Renderer> Row<'a, Message, Renderer> { /// Sets the padding of the [`Row`]. /// /// [`Row`]: struct.Row.html - pub fn padding(mut self, units: u16) -> Self { + pub const fn padding(mut self, units: u16) -> Self { self.padding = units; self } @@ -62,7 +62,7 @@ impl<'a, Message, Renderer> Row<'a, Message, Renderer> { /// Sets the width of the [`Row`]. /// /// [`Row`]: struct.Row.html - pub fn width(mut self, width: Length) -> Self { + pub const fn width(mut self, width: Length) -> Self { self.width = width; self } @@ -70,7 +70,7 @@ impl<'a, Message, Renderer> Row<'a, Message, Renderer> { /// Sets the height of the [`Row`]. /// /// [`Row`]: struct.Row.html - pub fn height(mut self, height: Length) -> Self { + pub const fn height(mut self, height: Length) -> Self { self.height = height; self } @@ -78,7 +78,7 @@ impl<'a, Message, Renderer> Row<'a, Message, Renderer> { /// Sets the maximum width of the [`Row`]. /// /// [`Row`]: struct.Row.html - pub fn max_width(mut self, max_width: u32) -> Self { + pub const fn max_width(mut self, max_width: u32) -> Self { self.max_width = max_width; self } @@ -86,7 +86,7 @@ impl<'a, Message, Renderer> Row<'a, Message, Renderer> { /// Sets the maximum height of the [`Row`]. /// /// [`Row`]: struct.Row.html - pub fn max_height(mut self, max_height: u32) -> Self { + pub const fn max_height(mut self, max_height: u32) -> Self { self.max_height = max_height; self } @@ -94,7 +94,7 @@ impl<'a, Message, Renderer> Row<'a, Message, Renderer> { /// Sets the vertical alignment of the contents of the [`Row`] . /// /// [`Row`]: struct.Row.html - pub fn align_items(mut self, align: Align) -> Self { + pub const fn align_items(mut self, align: Align) -> Self { self.align_items = align; self } diff --git a/native/src/widget/slider.rs b/native/src/widget/slider.rs index f07ea7cd..a8915da1 100644 --- a/native/src/widget/slider.rs +++ b/native/src/widget/slider.rs @@ -95,8 +95,8 @@ impl State { /// Creates a new [`State`]. /// /// [`State`]: struct.State.html - pub fn new() -> State { - State::default() + pub const fn new() -> State { + State { is_dragging: false } } } diff --git a/native/src/widget/text_input.rs b/native/src/widget/text_input.rs index f97ed424..0246f0d5 100644 --- a/native/src/widget/text_input.rs +++ b/native/src/widget/text_input.rs @@ -326,14 +326,17 @@ impl State { /// Creates a new [`State`], representing an unfocused [`TextInput`]. /// /// [`State`]: struct.State.html - pub fn new() -> Self { - Self::default() + pub const fn new() -> Self { + Self { + is_focused: false, + cursor_position: 0, + } } /// Creates a new [`State`], representing a focused [`TextInput`]. /// /// [`State`]: struct.State.html - pub fn focused() -> Self { + pub const fn focused() -> Self { use std::usize; Self { @@ -345,7 +348,7 @@ impl State { /// Returns whether the [`TextInput`] is currently focused or not. /// /// [`TextInput`]: struct.TextInput.html - pub fn is_focused(&self) -> bool { + pub const fn is_focused(&self) -> bool { self.is_focused } |