Skip to content

Commit b0fc94b

Browse files
committed
Allow TryInto for selection
1 parent 6c1ca84 commit b0fc94b

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

src/hl/container.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::convert::TryInto;
12
use std::fmt::{self, Debug};
23
use std::mem;
34
use std::ops::Deref;
@@ -67,12 +68,16 @@ impl<'a> Reader<'a> {
6768
pub fn read_slice<T, S, D>(&self, selection: S) -> Result<Array<T, D>>
6869
where
6970
T: H5Type,
70-
S: Into<Selection>,
71+
S: TryInto<Selection>,
7172
D: ndarray::Dimension,
7273
{
7374
ensure!(!self.obj.is_attr(), "Slicing cannot be used on attribute datasets");
7475

75-
let selection = selection.into();
76+
let selection = if let Ok(selection) = selection.try_into() {
77+
selection
78+
} else {
79+
fail!("Selection is not valid for hdf5")
80+
};
7681
let obj_space = self.obj.space()?;
7782

7883
let out_shape = selection.out_shape(&obj_space.shape())?;
@@ -145,7 +150,7 @@ impl<'a> Reader<'a> {
145150
pub fn read_slice_1d<T, S>(&self, selection: S) -> Result<Array1<T>>
146151
where
147152
T: H5Type,
148-
S: Into<Selection>,
153+
S: TryInto<Selection>,
149154
{
150155
self.read_slice(selection)
151156
}
@@ -162,7 +167,7 @@ impl<'a> Reader<'a> {
162167
pub fn read_slice_2d<T, S>(&self, selection: S) -> Result<Array2<T>>
163168
where
164169
T: H5Type,
165-
S: Into<Selection>,
170+
S: TryInto<Selection>,
166171
{
167172
self.read_slice(selection)
168173
}
@@ -234,12 +239,16 @@ impl<'a> Writer<'a> {
234239
where
235240
A: Into<ArrayView<'b, T, D>>,
236241
T: H5Type,
237-
S: Into<Selection>,
242+
S: TryInto<Selection>,
238243
D: ndarray::Dimension,
239244
{
240245
ensure!(!self.obj.is_attr(), "Slicing cannot be used on attribute datasets");
241246

242-
let selection = selection.into();
247+
let selection = if let Ok(selection) = selection.try_into() {
248+
selection
249+
} else {
250+
fail!("Selection is not valid for hdf5")
251+
};
243252
let obj_space = self.obj.space()?;
244253

245254
let out_shape = selection.out_shape(&obj_space.shape())?;
@@ -465,7 +474,7 @@ impl Container {
465474
pub fn read_slice_1d<T, S>(&self, selection: S) -> Result<Array1<T>>
466475
where
467476
T: H5Type,
468-
S: Into<Selection>,
477+
S: TryInto<Selection>,
469478
{
470479
self.as_reader().read_slice_1d(selection)
471480
}
@@ -482,7 +491,7 @@ impl Container {
482491
pub fn read_slice_2d<T, S>(&self, selection: S) -> Result<Array2<T>>
483492
where
484493
T: H5Type,
485-
S: Into<Selection>,
494+
S: TryInto<Selection>,
486495
{
487496
self.as_reader().read_slice_2d(selection)
488497
}
@@ -500,7 +509,7 @@ impl Container {
500509
pub fn read_slice<T, S, D>(&self, selection: S) -> Result<Array<T, D>>
501510
where
502511
T: H5Type,
503-
S: Into<Selection>,
512+
S: TryInto<Selection>,
504513
D: ndarray::Dimension,
505514
{
506515
self.as_reader().read_slice(selection)
@@ -546,7 +555,7 @@ impl Container {
546555
where
547556
A: Into<ArrayView<'b, T, D>>,
548557
T: H5Type,
549-
S: Into<Selection>,
558+
S: TryInto<Selection>,
550559
D: ndarray::Dimension,
551560
{
552561
self.as_writer().write_slice(arr, selection)

0 commit comments

Comments
 (0)