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