Skip to content

Commit c32f38e

Browse files
committed
Added from_parts_with_err method to Array
Signed-off-by: chandr-andr (Kiselev Aleksandr) <chandr@chandr.net>
1 parent a1a242d commit c32f38e

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/array.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::fmt;
2+
use std::io::{Error, ErrorKind};
23
use std::ops::{Index, IndexMut};
34
use std::slice;
45
use std::vec;
@@ -79,6 +80,22 @@ impl<T> Array<T> {
7980
}
8081
}
8182

83+
pub fn from_parts_with_err(
84+
data: Vec<T>,
85+
dimensions: Vec<Dimension>,
86+
) -> Result<Array<T>, Error> {
87+
if (data.is_empty() && dimensions.is_empty())
88+
|| data.len() as i32 == dimensions.iter().fold(1, |acc, i| acc * i.len)
89+
{
90+
return Err(Error::new(ErrorKind::Other, "size mismatch"));
91+
}
92+
93+
return Ok(Array {
94+
dims: dimensions,
95+
data,
96+
});
97+
}
98+
8299
/// Creates a new one-dimensional array.
83100
pub fn from_vec(data: Vec<T>, lower_bound: i32) -> Array<T> {
84101
Array {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy