Skip to main content

iced_core/
lib.rs

1//! The core library of [Iced].
2//!
3//! This library holds basic types that can be reused and re-exported in
4//! different runtime implementations.
5//!
6//! ![The foundations of the Iced ecosystem](https://github.com/iced-rs/iced/blob/0525d76ff94e828b7b21634fa94a747022001c83/docs/graphs/foundations.png?raw=true)
7//!
8//! [Iced]: https://github.com/iced-rs/iced
9#![doc(
10    html_logo_url = "https://raw.githubusercontent.com/iced-rs/iced/9ab6923e943f784985e9ef9ca28b10278297225d/docs/logo.svg"
11)]
12pub mod alignment;
13pub mod animation;
14pub mod backend;
15pub mod border;
16pub mod clipboard;
17pub mod color;
18pub mod event;
19pub mod font;
20pub mod gradient;
21pub mod image;
22pub mod input_method;
23pub mod keyboard;
24pub mod layout;
25pub mod mouse;
26pub mod overlay;
27pub mod padding;
28pub mod renderer;
29pub mod shell;
30pub mod svg;
31pub mod text;
32pub mod theme;
33pub mod time;
34pub mod touch;
35pub mod widget;
36pub mod window;
37
38mod angle;
39mod background;
40mod content_fit;
41mod element;
42mod length;
43mod pixels;
44mod point;
45mod rectangle;
46mod rotation;
47mod settings;
48mod shadow;
49mod size;
50mod transformation;
51mod vector;
52
53pub use alignment::Alignment;
54pub use angle::{Degrees, Radians};
55pub use animation::Animation;
56pub use backend::Backend;
57pub use background::Background;
58pub use border::Border;
59pub use clipboard::Clipboard;
60pub use color::Color;
61pub use content_fit::ContentFit;
62pub use element::Element;
63pub use event::Event;
64pub use font::Font;
65pub use gradient::Gradient;
66pub use image::Image;
67pub use input_method::InputMethod;
68pub use layout::Layout;
69pub use length::Length;
70pub use overlay::Overlay;
71pub use padding::Padding;
72pub use pixels::Pixels;
73pub use point::Point;
74pub use rectangle::Rectangle;
75pub use renderer::Renderer;
76pub use rotation::Rotation;
77pub use settings::Settings;
78pub use shadow::Shadow;
79pub use shell::Shell;
80pub use size::Size;
81pub use svg::Svg;
82pub use text::Text;
83pub use theme::Theme;
84pub use transformation::Transformation;
85pub use vector::Vector;
86pub use widget::Widget;
87pub use window::Window;
88
89pub use bytes::Bytes;
90pub use smol_str::SmolStr;
91pub use std::convert::Infallible as Never;
92
93/// A function that can _never_ be called.
94///
95/// This is useful to turn generic types into anything
96/// you want by coercing them into a type with no possible
97/// values.
98pub fn never<T>(never: Never) -> T {
99    match never {}
100}
101
102/// A trait extension for binary functions (`Fn(A, B) -> O`).
103///
104/// It enables you to use a bunch of nifty functional programming paradigms
105/// that work well with iced.
106pub trait Function<A, B, O> {
107    /// Applies the given first argument to a binary function and returns
108    /// a new function that takes the other argument.
109    ///
110    /// This lets you partially "apply" a function—equivalent to currying,
111    /// but it only works with binary functions. If you want to apply an
112    /// arbitrary number of arguments, create a little struct for them.
113    ///
114    /// # When is this useful?
115    /// Sometimes you will want to identify the source or target
116    /// of some message in your user interface. This can be achieved through
117    /// normal means by defining a closure and moving the identifier
118    /// inside:
119    ///
120    /// ```rust
121    /// # let element: Option<()> = Some(());
122    /// # enum Message { ButtonPressed(u32, ()) }
123    /// let id = 123;
124    ///
125    /// # let _ = {
126    /// element.map(move |result| Message::ButtonPressed(id, result))
127    /// # };
128    /// ```
129    ///
130    /// That's quite a mouthful. [`with`](Self::with) lets you write:
131    ///
132    /// ```rust
133    /// # use iced_core::Function;
134    /// # let element: Option<()> = Some(());
135    /// # enum Message { ButtonPressed(u32, ()) }
136    /// let id = 123;
137    ///
138    /// # let _ = {
139    /// element.map(Message::ButtonPressed.with(id))
140    /// # };
141    /// ```
142    ///
143    /// Effectively creating the same closure that partially applies
144    /// the `id` to the message—but much more concise!
145    fn with(self, prefix: A) -> impl Fn(B) -> O;
146}
147
148impl<F, A, B, O> Function<A, B, O> for F
149where
150    F: Fn(A, B) -> O,
151    Self: Sized,
152    A: Clone,
153{
154    fn with(self, prefix: A) -> impl Fn(B) -> O {
155        move |result| self(prefix.clone(), result)
156    }
157}