1use crate::{Quantity, Unit};
25use qtty_derive::Unit;
26
27pub use crate::dimension::Pressure;
29
30pub trait PressureUnit: Unit<Dim = Pressure> {}
32impl<T: Unit<Dim = Pressure>> PressureUnit for T {}
33
34#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Unit)]
42#[unit(symbol = "Pa", dimension = Pressure, ratio = 1.0)]
43pub struct Pascal;
44pub type Pa = Pascal;
46pub type Pascals = Quantity<Pa>;
48pub const PASCAL: Pascals = Pascals::new(1.0);
50
51macro_rules! si_pascal {
52 ($name:ident, $sym:literal, $ratio:expr, $alias:ident, $qty:ident, $one:ident) => {
53 #[doc = concat!("SI-prefixed pascal unit (", stringify!($ratio), " Pa).")]
54 #[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Unit)]
55 #[unit(symbol = $sym, dimension = Pressure, ratio = $ratio)]
56 pub struct $name;
57 #[doc = concat!("Type alias shorthand for [`", stringify!($name), "`].")]
58 pub type $alias = $name;
59 #[doc = concat!("A quantity measured in ", stringify!($name), "s.")]
60 pub type $qty = Quantity<$alias>;
61 #[doc = concat!("One ", stringify!($name), ".")]
62 pub const $one: $qty = $qty::new(1.0);
63 };
64}
65
66#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Unit)]
71#[unit(symbol = "hPa", dimension = Pressure, ratio = 1e2)]
72pub struct Hectopascal;
73pub type HPa = Hectopascal;
75pub type Hectopascals = Quantity<HPa>;
77pub const HECTOPASCAL: Hectopascals = Hectopascals::new(1.0);
79
80si_pascal!(Millipascal, "mPa", 1e-3, MilliPa, Millipascals, MILLIPASCAL);
81si_pascal!(Kilopascal, "kPa", 1e3, KPa, Kilopascals, KILOPASCAL);
82si_pascal!(Megapascal, "MPa", 1e6, MPa, Megapascals, MEGAPASCAL);
83si_pascal!(Gigapascal, "GPa", 1e9, GPa, Gigapascals, GIGAPASCAL);
84
85#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Unit)]
95#[unit(symbol = "bar", dimension = Pressure, ratio = 1e5)]
96pub struct Bar;
97pub type Bars = Quantity<Bar>;
99pub const BAR: Bars = Bars::new(1.0);
101
102#[cfg(feature = "customary")]
108#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Unit)]
109#[unit(symbol = "atm", dimension = Pressure, ratio = 101_325.0)]
110pub struct Atmosphere;
111#[cfg(feature = "customary")]
113pub type Atm = Atmosphere;
114#[cfg(feature = "customary")]
116pub type Atmospheres = Quantity<Atm>;
117#[cfg(feature = "customary")]
119pub const ATMOSPHERE: Atmospheres = Atmospheres::new(1.0);
120
121#[cfg(feature = "customary")]
125#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Unit)]
126#[unit(symbol = "Torr", dimension = Pressure, ratio = 101_325.0 / 760.0)]
127pub struct Torr;
128#[cfg(feature = "customary")]
130pub type Torrs = Quantity<Torr>;
131#[cfg(feature = "customary")]
133pub const TORR: Torrs = Torrs::new(1.0);
134
135#[cfg(feature = "customary")]
137#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Unit)]
138#[unit(symbol = "mmHg", dimension = Pressure, ratio = 101_325.0 / 760.0)]
139pub struct MillimeterOfMercury;
140#[cfg(feature = "customary")]
142pub type MmHg = MillimeterOfMercury;
143#[cfg(feature = "customary")]
145pub type MillimetersOfMercury = Quantity<MmHg>;
146#[cfg(feature = "customary")]
148pub const MILLIMETER_OF_MERCURY: MillimetersOfMercury = MillimetersOfMercury::new(1.0);
149
150#[cfg(feature = "customary")]
152#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Unit)]
153#[unit(symbol = "psi", dimension = Pressure, ratio = 6_894.757_293)]
154pub struct PoundPerSquareInch;
155#[cfg(feature = "customary")]
157pub type Psi = PoundPerSquareInch;
158#[cfg(feature = "customary")]
160pub type PoundsPerSquareInch = Quantity<Psi>;
161#[cfg(feature = "customary")]
163pub const PSI: PoundsPerSquareInch = PoundsPerSquareInch::new(1.0);
164
165#[cfg(feature = "customary")]
167#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Unit)]
168#[unit(symbol = "inHg", dimension = Pressure, ratio = 3_386.389)]
169pub struct InchOfMercury;
170#[cfg(feature = "customary")]
172pub type InHg = InchOfMercury;
173#[cfg(feature = "customary")]
175pub type InchesOfMercury = Quantity<InHg>;
176#[cfg(feature = "customary")]
178pub const INCH_OF_MERCURY: InchesOfMercury = InchesOfMercury::new(1.0);
179
180#[macro_export]
186#[doc(hidden)]
187macro_rules! pressure_units {
188 ($cb:path) => {
189 $cb!(
190 Pascal,
191 Millipascal,
192 Hectopascal,
193 Kilopascal,
194 Megapascal,
195 Gigapascal,
196 Bar
197 );
198 };
199}
200
201#[cfg(feature = "customary")]
203#[macro_export]
204#[doc(hidden)]
205macro_rules! pressure_customary_units {
206 ($cb:path) => {
207 $cb!(
208 Atmosphere,
209 Torr,
210 MillimeterOfMercury,
211 PoundPerSquareInch,
212 InchOfMercury
213 );
214 };
215}
216
217pressure_units!(crate::impl_unit_from_conversions);
219
220#[cfg(feature = "cross-unit-ops")]
221pressure_units!(crate::impl_unit_cross_unit_ops);
222
223#[cfg(feature = "customary")]
227crate::impl_unit_from_conversions_between!(
228 Pascal, Millipascal, Hectopascal, Kilopascal, Megapascal, Gigapascal, Bar;
229 Atmosphere, Torr, MillimeterOfMercury, PoundPerSquareInch, InchOfMercury
230);
231
232#[cfg(all(feature = "customary", feature = "cross-unit-ops"))]
233crate::impl_unit_cross_unit_ops_between!(
234 Pascal, Millipascal, Hectopascal, Kilopascal, Megapascal, Gigapascal, Bar;
235 Atmosphere, Torr, MillimeterOfMercury, PoundPerSquareInch, InchOfMercury
236);
237
238#[cfg(test)]
240pressure_units!(crate::assert_units_are_builtin);
241
242#[cfg(all(test, feature = "std"))]
243mod tests {
244 use super::*;
245 use approx::assert_abs_diff_eq;
246
247 #[test]
248 fn pascal_to_hectopascal() {
249 let p = Pascals::new(101_325.0);
250 let hpa: Hectopascals = p.to();
251 assert_abs_diff_eq!(hpa.value(), 1013.25, epsilon = 1e-9);
252 }
253
254 #[test]
255 fn hectopascal_to_pascal() {
256 let hpa = Hectopascals::new(1013.25);
257 let p: Pascals = hpa.to();
258 assert_abs_diff_eq!(p.value(), 101_325.0, epsilon = 1e-9);
259 }
260
261 #[test]
262 fn pascal_to_kilopascal() {
263 let p = Pascals::new(1000.0);
264 let kpa: Kilopascals = p.to();
265 assert_abs_diff_eq!(kpa.value(), 1.0, epsilon = 1e-12);
266 }
267
268 #[test]
269 fn pascal_to_bar() {
270 let p = Pascals::new(100_000.0);
271 let bar: Bars = p.to();
272 assert_abs_diff_eq!(bar.value(), 1.0, epsilon = 1e-12);
273 }
274
275 #[test]
276 fn bar_to_hectopascal() {
277 let bar = Bars::new(1.0);
278 let hpa: Hectopascals = bar.to();
279 assert_abs_diff_eq!(hpa.value(), 1000.0, epsilon = 1e-9);
280 }
281
282 #[test]
283 fn pascals_addition() {
284 let a = Pascals::new(50.0);
285 let b = Pascals::new(50.0);
286 let c = a + b;
287 assert_abs_diff_eq!(c.value(), 100.0, epsilon = 1e-12);
288 }
289
290 #[test]
291 #[cfg(feature = "customary")]
292 fn atmosphere_to_pascal() {
293 let atm = Atmospheres::new(1.0);
294 let p: Pascals = atm.to();
295 assert_abs_diff_eq!(p.value(), 101_325.0, epsilon = 1e-9);
296 }
297
298 #[test]
299 #[cfg(feature = "customary")]
300 fn torr_to_atmosphere() {
301 let t = Torrs::new(760.0);
303 let a: Atmospheres = t.to();
304 assert_abs_diff_eq!(a.value(), 1.0, epsilon = 1e-12);
305 }
306
307 #[test]
308 #[cfg(feature = "customary")]
309 fn psi_to_pascal() {
310 let p = PoundsPerSquareInch::new(1.0);
311 let pa: Pascals = p.to();
312 assert_abs_diff_eq!(pa.value(), 6_894.757_293, epsilon = 1e-6);
313 }
314}