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`.
This commit is contained in:
Nikolai Vazquez 2019-11-29 21:24:52 -05:00
parent 811d8b90d7
commit 267e242238
16 changed files with 60 additions and 57 deletions

View File

@ -14,7 +14,7 @@ impl Point {
/// Creates a new [`Point`] with the given coordinates. /// Creates a new [`Point`] with the given coordinates.
/// ///
/// [`Point`]: struct.Point.html /// [`Point`]: struct.Point.html
pub fn new(x: f32, y: f32) -> Self { pub const fn new(x: f32, y: f32) -> Self {
Self { x, y } Self { x, y }
} }
} }

View File

@ -16,7 +16,7 @@ impl<T> Vector<T> {
/// Creates a new [`Vector`] with the given components. /// Creates a new [`Vector`] with the given components.
/// ///
/// [`Vector`]: struct.Vector.html /// [`Vector`]: struct.Vector.html
pub fn new(x: T, y: T) -> Self { pub const fn new(x: T, y: T) -> Self {
Self { x, y } Self { x, y }
} }
} }

View File

@ -20,7 +20,7 @@ impl Limits {
/// ///
/// [`Limits`]: struct.Limits.html /// [`Limits`]: struct.Limits.html
/// [`Size`]: ../struct.Size.html /// [`Size`]: ../struct.Size.html
pub fn new(min: Size, max: Size) -> Limits { pub const fn new(min: Size, max: Size) -> Limits {
Limits { Limits {
min, min,
max, max,
@ -32,7 +32,7 @@ impl Limits {
/// ///
/// [`Limits`]: struct.Limits.html /// [`Limits`]: struct.Limits.html
/// [`Size`]: ../struct.Size.html /// [`Size`]: ../struct.Size.html
pub fn min(&self) -> Size { pub const fn min(&self) -> Size {
self.min self.min
} }
@ -40,7 +40,7 @@ impl Limits {
/// ///
/// [`Limits`]: struct.Limits.html /// [`Limits`]: struct.Limits.html
/// [`Size`]: ../struct.Size.html /// [`Size`]: ../struct.Size.html
pub fn max(&self) -> Size { pub const fn max(&self) -> Size {
self.max self.max
} }

View File

@ -12,7 +12,7 @@ impl Node {
/// ///
/// [`Node`]: struct.Node.html /// [`Node`]: struct.Node.html
/// [`Size`]: ../struct.Size.html /// [`Size`]: ../struct.Size.html
pub fn new(size: Size) -> Self { pub const fn new(size: Size) -> Self {
Self::with_children(size, Vec::new()) Self::with_children(size, Vec::new())
} }
@ -20,7 +20,7 @@ impl Node {
/// ///
/// [`Node`]: struct.Node.html /// [`Node`]: struct.Node.html
/// [`Size`]: ../struct.Size.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 { Node {
bounds: Rectangle { bounds: Rectangle {
x: 0.0, x: 0.0,
@ -36,14 +36,14 @@ impl Node {
/// ///
/// [`Node`]: struct.Node.html /// [`Node`]: struct.Node.html
/// [`Size`]: ../struct.Size.html /// [`Size`]: ../struct.Size.html
pub fn size(&self) -> Size { pub const fn size(&self) -> Size {
Size::new(self.bounds.width, self.bounds.height) Size::new(self.bounds.width, self.bounds.height)
} }
/// Returns the bounds of the [`Node`]. /// Returns the bounds of the [`Node`].
/// ///
/// [`Node`]: struct.Node.html /// [`Node`]: struct.Node.html
pub fn bounds(&self) -> Rectangle { pub const fn bounds(&self) -> Rectangle {
self.bounds self.bounds
} }

View File

@ -28,7 +28,7 @@ impl<'a, Message, Renderer> Column<'a, Message, Renderer> {
/// Creates an empty [`Column`]. /// Creates an empty [`Column`].
/// ///
/// [`Column`]: struct.Column.html /// [`Column`]: struct.Column.html
pub fn new() -> Self { pub const fn new() -> Self {
Column { Column {
spacing: 0, spacing: 0,
padding: 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 /// Custom margins per element do not exist in Iced. You should use this
/// method instead! While less flexible, it helps you keep spacing between /// method instead! While less flexible, it helps you keep spacing between
/// elements consistent. /// elements consistent.
pub fn spacing(mut self, units: u16) -> Self { pub const fn spacing(mut self, units: u16) -> Self {
self.spacing = units; self.spacing = units;
self self
} }
@ -54,7 +54,7 @@ impl<'a, Message, Renderer> Column<'a, Message, Renderer> {
/// Sets the padding of the [`Column`]. /// Sets the padding of the [`Column`].
/// ///
/// [`Column`]: struct.Column.html /// [`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.padding = units;
self self
} }
@ -62,7 +62,7 @@ impl<'a, Message, Renderer> Column<'a, Message, Renderer> {
/// Sets the width of the [`Column`]. /// Sets the width of the [`Column`].
/// ///
/// [`Column`]: struct.Column.html /// [`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.width = width;
self self
} }
@ -70,7 +70,7 @@ impl<'a, Message, Renderer> Column<'a, Message, Renderer> {
/// Sets the height of the [`Column`]. /// Sets the height of the [`Column`].
/// ///
/// [`Column`]: struct.Column.html /// [`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.height = height;
self self
} }
@ -78,7 +78,7 @@ impl<'a, Message, Renderer> Column<'a, Message, Renderer> {
/// Sets the maximum width of the [`Column`]. /// Sets the maximum width of the [`Column`].
/// ///
/// [`Column`]: struct.Column.html /// [`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.max_width = max_width;
self self
} }
@ -86,7 +86,7 @@ impl<'a, Message, Renderer> Column<'a, Message, Renderer> {
/// Sets the maximum height of the [`Column`] in pixels. /// Sets the maximum height of the [`Column`] in pixels.
/// ///
/// [`Column`]: struct.Column.html /// [`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.max_height = max_height;
self self
} }
@ -94,7 +94,7 @@ impl<'a, Message, Renderer> Column<'a, Message, Renderer> {
/// Sets the horizontal alignment of the contents of the [`Column`] . /// Sets the horizontal alignment of the contents of the [`Column`] .
/// ///
/// [`Column`]: struct.Column.html /// [`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.align_items = align;
self self
} }

View File

@ -37,7 +37,7 @@ impl Image {
/// Sets the width of the [`Image`] boundaries. /// Sets the width of the [`Image`] boundaries.
/// ///
/// [`Image`]: struct.Image.html /// [`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.width = width;
self self
} }
@ -45,7 +45,7 @@ impl Image {
/// Sets the height of the [`Image`] boundaries. /// Sets the height of the [`Image`] boundaries.
/// ///
/// [`Image`]: struct.Image.html /// [`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.height = height;
self self
} }

View File

@ -28,7 +28,7 @@ impl<'a, Message, Renderer> Row<'a, Message, Renderer> {
/// Creates an empty [`Row`]. /// Creates an empty [`Row`].
/// ///
/// [`Row`]: struct.Row.html /// [`Row`]: struct.Row.html
pub fn new() -> Self { pub const fn new() -> Self {
Row { Row {
spacing: 0, spacing: 0,
padding: 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 /// Custom margins per element do not exist in Iced. You should use this
/// method instead! While less flexible, it helps you keep spacing between /// method instead! While less flexible, it helps you keep spacing between
/// elements consistent. /// elements consistent.
pub fn spacing(mut self, units: u16) -> Self { pub const fn spacing(mut self, units: u16) -> Self {
self.spacing = units; self.spacing = units;
self self
} }
@ -54,7 +54,7 @@ impl<'a, Message, Renderer> Row<'a, Message, Renderer> {
/// Sets the padding of the [`Row`]. /// Sets the padding of the [`Row`].
/// ///
/// [`Row`]: struct.Row.html /// [`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.padding = units;
self self
} }
@ -62,7 +62,7 @@ impl<'a, Message, Renderer> Row<'a, Message, Renderer> {
/// Sets the width of the [`Row`]. /// Sets the width of the [`Row`].
/// ///
/// [`Row`]: struct.Row.html /// [`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.width = width;
self self
} }
@ -70,7 +70,7 @@ impl<'a, Message, Renderer> Row<'a, Message, Renderer> {
/// Sets the height of the [`Row`]. /// Sets the height of the [`Row`].
/// ///
/// [`Row`]: struct.Row.html /// [`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.height = height;
self self
} }
@ -78,7 +78,7 @@ impl<'a, Message, Renderer> Row<'a, Message, Renderer> {
/// Sets the maximum width of the [`Row`]. /// Sets the maximum width of the [`Row`].
/// ///
/// [`Row`]: struct.Row.html /// [`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.max_width = max_width;
self self
} }
@ -86,7 +86,7 @@ impl<'a, Message, Renderer> Row<'a, Message, Renderer> {
/// Sets the maximum height of the [`Row`]. /// Sets the maximum height of the [`Row`].
/// ///
/// [`Row`]: struct.Row.html /// [`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.max_height = max_height;
self self
} }
@ -94,7 +94,7 @@ impl<'a, Message, Renderer> Row<'a, Message, Renderer> {
/// Sets the vertical alignment of the contents of the [`Row`] . /// Sets the vertical alignment of the contents of the [`Row`] .
/// ///
/// [`Row`]: struct.Row.html /// [`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.align_items = align;
self self
} }

View File

@ -95,8 +95,8 @@ impl State {
/// Creates a new [`State`]. /// Creates a new [`State`].
/// ///
/// [`State`]: struct.State.html /// [`State`]: struct.State.html
pub fn new() -> State { pub const fn new() -> State {
State::default() State { is_dragging: false }
} }
} }

View File

@ -326,14 +326,17 @@ impl State {
/// Creates a new [`State`], representing an unfocused [`TextInput`]. /// Creates a new [`State`], representing an unfocused [`TextInput`].
/// ///
/// [`State`]: struct.State.html /// [`State`]: struct.State.html
pub fn new() -> Self { pub const fn new() -> Self {
Self::default() Self {
is_focused: false,
cursor_position: 0,
}
} }
/// Creates a new [`State`], representing a focused [`TextInput`]. /// Creates a new [`State`], representing a focused [`TextInput`].
/// ///
/// [`State`]: struct.State.html /// [`State`]: struct.State.html
pub fn focused() -> Self { pub const fn focused() -> Self {
use std::usize; use std::usize;
Self { Self {
@ -345,7 +348,7 @@ impl State {
/// Returns whether the [`TextInput`] is currently focused or not. /// Returns whether the [`TextInput`] is currently focused or not.
/// ///
/// [`TextInput`]: struct.TextInput.html /// [`TextInput`]: struct.TextInput.html
pub fn is_focused(&self) -> bool { pub const fn is_focused(&self) -> bool {
self.is_focused self.is_focused
} }

View File

@ -113,8 +113,8 @@ impl State {
/// Creates a new [`State`]. /// Creates a new [`State`].
/// ///
/// [`State`]: struct.State.html /// [`State`]: struct.State.html
pub fn new() -> State { pub const fn new() -> State {
State::default() State
} }
} }

View File

@ -24,7 +24,7 @@ impl<'a, Message> Column<'a, Message> {
/// Creates an empty [`Column`]. /// Creates an empty [`Column`].
/// ///
/// [`Column`]: struct.Column.html /// [`Column`]: struct.Column.html
pub fn new() -> Self { pub const fn new() -> Self {
Column { Column {
spacing: 0, spacing: 0,
padding: 0, padding: 0,
@ -42,7 +42,7 @@ impl<'a, Message> Column<'a, Message> {
/// Custom margins per element do not exist in Iced. You should use this /// Custom margins per element do not exist in Iced. You should use this
/// method instead! While less flexible, it helps you keep spacing between /// method instead! While less flexible, it helps you keep spacing between
/// elements consistent. /// elements consistent.
pub fn spacing(mut self, units: u16) -> Self { pub const fn spacing(mut self, units: u16) -> Self {
self.spacing = units; self.spacing = units;
self self
} }
@ -50,7 +50,7 @@ impl<'a, Message> Column<'a, Message> {
/// Sets the padding of the [`Column`]. /// Sets the padding of the [`Column`].
/// ///
/// [`Column`]: struct.Column.html /// [`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.padding = units;
self self
} }
@ -58,7 +58,7 @@ impl<'a, Message> Column<'a, Message> {
/// Sets the width of the [`Column`]. /// Sets the width of the [`Column`].
/// ///
/// [`Column`]: struct.Column.html /// [`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.width = width;
self self
} }
@ -66,7 +66,7 @@ impl<'a, Message> Column<'a, Message> {
/// Sets the height of the [`Column`]. /// Sets the height of the [`Column`].
/// ///
/// [`Column`]: struct.Column.html /// [`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.height = height;
self self
} }
@ -74,7 +74,7 @@ impl<'a, Message> Column<'a, Message> {
/// Sets the maximum width of the [`Column`]. /// Sets the maximum width of the [`Column`].
/// ///
/// [`Column`]: struct.Column.html /// [`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.max_width = max_width;
self self
} }
@ -82,7 +82,7 @@ impl<'a, Message> Column<'a, Message> {
/// Sets the maximum height of the [`Column`] in pixels. /// Sets the maximum height of the [`Column`] in pixels.
/// ///
/// [`Column`]: struct.Column.html /// [`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.max_height = max_height;
self self
} }
@ -90,7 +90,7 @@ impl<'a, Message> Column<'a, Message> {
/// Sets the horizontal alignment of the contents of the [`Column`] . /// Sets the horizontal alignment of the contents of the [`Column`] .
/// ///
/// [`Column`]: struct.Column.html /// [`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.align_items = align;
self self
} }

View File

@ -24,7 +24,7 @@ impl<'a, Message> Row<'a, Message> {
/// Creates an empty [`Row`]. /// Creates an empty [`Row`].
/// ///
/// [`Row`]: struct.Row.html /// [`Row`]: struct.Row.html
pub fn new() -> Self { pub const fn new() -> Self {
Row { Row {
spacing: 0, spacing: 0,
padding: 0, padding: 0,
@ -42,7 +42,7 @@ impl<'a, Message> Row<'a, Message> {
/// Custom margins per element do not exist in Iced. You should use this /// Custom margins per element do not exist in Iced. You should use this
/// method instead! While less flexible, it helps you keep spacing between /// method instead! While less flexible, it helps you keep spacing between
/// elements consistent. /// elements consistent.
pub fn spacing(mut self, units: u16) -> Self { pub const fn spacing(mut self, units: u16) -> Self {
self.spacing = units; self.spacing = units;
self self
} }
@ -50,7 +50,7 @@ impl<'a, Message> Row<'a, Message> {
/// Sets the padding of the [`Row`]. /// Sets the padding of the [`Row`].
/// ///
/// [`Row`]: struct.Row.html /// [`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.padding = units;
self self
} }
@ -58,7 +58,7 @@ impl<'a, Message> Row<'a, Message> {
/// Sets the width of the [`Row`]. /// Sets the width of the [`Row`].
/// ///
/// [`Row`]: struct.Row.html /// [`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.width = width;
self self
} }
@ -66,7 +66,7 @@ impl<'a, Message> Row<'a, Message> {
/// Sets the height of the [`Row`]. /// Sets the height of the [`Row`].
/// ///
/// [`Row`]: struct.Row.html /// [`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.height = height;
self self
} }
@ -74,7 +74,7 @@ impl<'a, Message> Row<'a, Message> {
/// Sets the maximum width of the [`Row`]. /// Sets the maximum width of the [`Row`].
/// ///
/// [`Row`]: struct.Row.html /// [`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.max_width = max_width;
self self
} }
@ -82,7 +82,7 @@ impl<'a, Message> Row<'a, Message> {
/// Sets the maximum height of the [`Row`]. /// Sets the maximum height of the [`Row`].
/// ///
/// [`Row`]: struct.Row.html /// [`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.max_height = max_height;
self self
} }
@ -90,7 +90,7 @@ impl<'a, Message> Row<'a, Message> {
/// Sets the vertical alignment of the contents of the [`Row`] . /// Sets the vertical alignment of the contents of the [`Row`] .
/// ///
/// [`Row`]: struct.Row.html /// [`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.align_items = align;
self self
} }

View File

@ -151,7 +151,7 @@ impl State {
/// Creates a new [`State`] with the scrollbar located at the top. /// Creates a new [`State`] with the scrollbar located at the top.
/// ///
/// [`State`]: struct.State.html /// [`State`]: struct.State.html
pub fn new() -> Self { pub const fn new() -> Self {
State::default() State
} }
} }

View File

@ -147,7 +147,7 @@ impl State {
/// Creates a new [`State`]. /// Creates a new [`State`].
/// ///
/// [`State`]: struct.State.html /// [`State`]: struct.State.html
pub fn new() -> Self { pub const fn new() -> Self {
Self Self
} }
} }

View File

@ -191,7 +191,7 @@ impl State {
/// Creates a new [`State`], representing an unfocused [`TextInput`]. /// Creates a new [`State`], representing an unfocused [`TextInput`].
/// ///
/// [`State`]: struct.State.html /// [`State`]: struct.State.html
pub fn new() -> Self { pub const fn new() -> Self {
Self::default() Self
} }
} }

View File

@ -35,7 +35,7 @@ struct Layer<'a> {
} }
impl<'a> Layer<'a> { impl<'a> Layer<'a> {
pub fn new(bounds: Rectangle<u32>, offset: Vector<u32>) -> Self { pub const fn new(bounds: Rectangle<u32>, offset: Vector<u32>) -> Self {
Self { Self {
bounds, bounds,
offset, offset,