Skip to content

Commit 6f3e884

Browse files
committed
Factor out floats in DynValue into DynFloat
1 parent 05a5074 commit 6f3e884

File tree

1 file changed

+60
-13
lines changed

1 file changed

+60
-13
lines changed

hdf5-types/src/dyn_value.rs

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -115,36 +115,86 @@ impl From<DynInteger> for DynValue<'_> {
115115
}
116116

117117
#[derive(Copy, Clone, PartialEq)]
118-
pub enum DynScalar {
119-
Integer(DynInteger),
118+
pub enum DynFloat {
120119
#[cfg(feature = "f16")]
121120
Float16(::half::f16),
122121
Float32(f32),
123122
Float64(f64),
124-
Boolean(bool),
125123
}
126124

127-
unsafe impl DynClone for DynScalar {
125+
impl DynFloat {
126+
pub(self) fn read(buf: &[u8], size: FloatSize) -> Self {
127+
match size {
128+
#[cfg(feature = "f16")]
129+
FloatSize::U2 => Self::Float16(read_raw(buf)),
130+
FloatSize::U4 => Self::Float32(read_raw(buf)),
131+
FloatSize::U8 => Self::Float64(read_raw(buf)),
132+
}
133+
}
134+
}
135+
136+
unsafe impl DynClone for DynFloat {
128137
fn dyn_clone(&mut self, out: &mut [u8]) {
129138
match self {
130-
Self::Integer(x) => x.dyn_clone(out),
131139
#[cfg(feature = "f16")]
132140
Self::Float16(x) => write_raw(out, *x),
133141
Self::Float32(x) => write_raw(out, *x),
134142
Self::Float64(x) => write_raw(out, *x),
135-
Self::Boolean(x) => write_raw(out, *x),
136143
}
137144
}
138145
}
139146

140-
impl Debug for DynScalar {
147+
impl Debug for DynFloat {
141148
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
142-
match self {
143-
Self::Integer(x) => Debug::fmt(&x, f),
149+
match *self {
144150
#[cfg(feature = "f16")]
145151
Self::Float16(x) => Debug::fmt(&x, f),
146152
Self::Float32(x) => Debug::fmt(&x, f),
147153
Self::Float64(x) => Debug::fmt(&x, f),
154+
}
155+
}
156+
}
157+
158+
impl Display for DynFloat {
159+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
160+
Debug::fmt(self, f)
161+
}
162+
}
163+
164+
impl From<DynFloat> for DynScalar {
165+
fn from(value: DynFloat) -> Self {
166+
Self::Float(value)
167+
}
168+
}
169+
170+
impl From<DynFloat> for DynValue<'_> {
171+
fn from(value: DynFloat) -> Self {
172+
DynScalar::Float(value).into()
173+
}
174+
}
175+
176+
#[derive(Copy, Clone, PartialEq)]
177+
pub enum DynScalar {
178+
Integer(DynInteger),
179+
Float(DynFloat),
180+
Boolean(bool),
181+
}
182+
183+
unsafe impl DynClone for DynScalar {
184+
fn dyn_clone(&mut self, out: &mut [u8]) {
185+
match self {
186+
Self::Integer(x) => x.dyn_clone(out),
187+
Self::Float(x) => x.dyn_clone(out),
188+
Self::Boolean(x) => write_raw(out, *x),
189+
}
190+
}
191+
}
192+
193+
impl Debug for DynScalar {
194+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
195+
match self {
196+
Self::Integer(x) => Debug::fmt(&x, f),
197+
Self::Float(x) => Debug::fmt(&x, f),
148198
Self::Boolean(x) => Debug::fmt(&x, f),
149199
}
150200
}
@@ -643,10 +693,7 @@ impl<'a> DynValue<'a> {
643693

644694
match tp {
645695
Integer(size) | Unsigned(size) => DynInteger::read(buf, true, *size).into(),
646-
#[cfg(feature = "f16")]
647-
Float(FloatSize::U2) => DynScalar::Float16(read_raw(buf)).into(),
648-
Float(FloatSize::U4) => DynScalar::Float32(read_raw(buf)).into(),
649-
Float(FloatSize::U8) => DynScalar::Float64(read_raw(buf)).into(),
696+
Float(size) => DynFloat::read(buf, *size).into(),
650697
Boolean => DynScalar::Boolean(read_raw(buf)).into(),
651698
Enum(ref tp) => DynEnum::new(tp, DynInteger::read(buf, tp.signed, tp.size)).into(),
652699
Compound(ref tp) => DynCompound::new(tp, buf).into(),

0 commit comments

Comments
 (0)