Skip to main content

co_primitives/types/
total_float.rs

1// SPDX-License-Identifier: AGPL-3.0-only
2// Copyright (C) 2026 1io BRANDGUARDIAN GmbH
3
4use derive_more::{From, Into};
5use ordered_float::OrderedFloat;
6use schemars::{
7	gen::SchemaGenerator,
8	schema::{InstanceType, Schema, SchemaObject},
9	JsonSchema,
10};
11use serde::{Deserialize, Serialize};
12use std::fmt::Display;
13
14/// f64 float wich uses total order from IEEE 754 (2008 revision).
15#[derive(Debug, Clone, Copy, Hash, From, Into, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
16#[serde(from = "f64", into = "f64")]
17#[repr(transparent)]
18pub struct TotalFloat64(OrderedFloat<f64>);
19impl TotalFloat64 {
20	pub fn value(&self) -> f64 {
21		self.0 .0
22	}
23}
24impl JsonSchema for TotalFloat64 {
25	fn schema_name() -> String {
26		"double".to_owned()
27	}
28
29	fn json_schema(_: &mut SchemaGenerator) -> Schema {
30		SchemaObject {
31			instance_type: Some(InstanceType::Number.into()),
32			format: Some("double".to_owned()),
33			..Default::default()
34		}
35		.into()
36	}
37}
38impl From<TotalFloat64> for f64 {
39	fn from(value: TotalFloat64) -> Self {
40		value.0.into()
41	}
42}
43impl From<f64> for TotalFloat64 {
44	fn from(value: f64) -> Self {
45		TotalFloat64(OrderedFloat(value))
46	}
47}
48impl AsRef<f64> for TotalFloat64 {
49	fn as_ref(&self) -> &f64 {
50		&self.0 .0
51	}
52}
53impl Display for TotalFloat64 {
54	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
55		write!(f, "{}", AsRef::<f64>::as_ref(self))
56	}
57}