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