pub enum DataVector {
I8(Vec<i8>),
U8(Vec<u8>),
I16(Vec<i16>),
I32(Vec<i32>),
F32(Vec<f32>),
F64(Vec<f64>),
}Expand description
Wraps the six NetCDF-3 data types.
It allows to load variable data from files easily through the methods:
§Example
use std::collections::HashMap;
use netcdf3::{FileReader, DataVector, DataType};
const LATITUDE_VAR_NAME: &str = "latitude";
const LATITUDE_VAR_DATA: [f32; 3] = [0.0, 0.5, 1.0];
const NUM_VARS: usize = 9;
// ...
// Read variable data from the file
// ---------------------------------
let mut file_reader: FileReader = FileReader::open(input_file_path).unwrap();
assert_eq!(NUM_VARS, file_reader.data_set().num_vars());
assert_eq!(true, file_reader.data_set().has_var(LATITUDE_VAR_NAME));
assert_eq!(DataType::F32, file_reader.data_set().var_data_type(LATITUDE_VAR_NAME).unwrap());
let mut data: HashMap<String, DataVector> = file_reader.read_all_vars().unwrap();
file_reader.close();
assert_eq!(NUM_VARS, data.len());
assert_eq!(true, data.contains_key(LATITUDE_VAR_NAME));
let latitude: DataVector = data.remove(LATITUDE_VAR_NAME).unwrap();
assert_eq!(DataType::F32, latitude.data_type());
let latitude: Vec<f32> = latitude.get_f32_into().unwrap();
assert_eq!(LATITUDE_VAR_DATA.to_vec(), latitude);Variants§
Implementations§
Source§impl DataVector
impl DataVector
Sourcepub fn get_i8(&self) -> Option<&[i8]>
pub fn get_i8(&self) -> Option<&[i8]>
Returns a slice to the internal Vec<i8>.
§Example
use netcdf3::{DataVector, DataType};
let data_vec = DataVector::I8(vec![1_i8, 2, 3]);
assert_eq!(DataType::I8, data_vec.data_type());
assert_eq!(Some(&[1_i8, 2, 3][..]), data_vec.get_i8());
assert_eq!(None, data_vec.get_u8());
assert_eq!(None, data_vec.get_i16());
assert_eq!(None, data_vec.get_i32());
assert_eq!(None, data_vec.get_f32());
assert_eq!(None, data_vec.get_f64());Sourcepub fn get_u8(&self) -> Option<&[u8]>
pub fn get_u8(&self) -> Option<&[u8]>
Returns a slice to the internal Vec<u8>.
Also see the method get_i8.
Sourcepub fn get_i16(&self) -> Option<&[i16]>
pub fn get_i16(&self) -> Option<&[i16]>
Returns a slice to the internal Vec<i16>.
Also see the method get_i8.
Sourcepub fn get_i32(&self) -> Option<&[i32]>
pub fn get_i32(&self) -> Option<&[i32]>
Returns a slice to the internal Vec<i32>.
Also see the method get_i8.
Sourcepub fn get_f32(&self) -> Option<&[f32]>
pub fn get_f32(&self) -> Option<&[f32]>
Returns a slice to the internal Vec<f32>.
Also see the method get_i8.
Sourcepub fn get_f64(&self) -> Option<&[f64]>
pub fn get_f64(&self) -> Option<&[f64]>
Returns a slice to the internal Vec<f64>.
Also see the method get_i8.
Sourcepub fn get_i8_into(self) -> Result<Vec<i8>, DataVector>
pub fn get_i8_into(self) -> Result<Vec<i8>, DataVector>
Returns the internal Vec<i8> if the DataVector contains one.
Otherwise the instance of the DataVector is returned as an errror.
§Example
use netcdf3::{DataVector, DataType};
let data_1: Vec<i8> = vec![1, 2 ,3];
let ptr_1 : *const i8 = data_1.as_ptr();
// Frirst create a `DataVector::I8`
let data_vec: DataVector = DataVector::I8(data_1);
assert_eq!(DataType::I8, data_vec.data_type());
// Try to extract the internal vector with the wrong data types
let data_vec: DataVector = data_vec.get_u8_into().unwrap_err();
let data_vec: DataVector = data_vec.get_i16_into().unwrap_err();
let data_vec: DataVector = data_vec.get_i32_into().unwrap_err();
let data_vec: DataVector = data_vec.get_f32_into().unwrap_err();
let data_vec: DataVector = data_vec.get_f64_into().unwrap_err();
// Extract the internal vector with the good data type
let data_2: Vec<i8> = data_vec.get_i8_into().unwrap();
let ptr_2 : *const i8 = data_2.as_ptr();
assert_eq!(vec![1, 2, 3], data_2);
// No copy of the buffer has been done
assert_eq!(ptr_1, ptr_2);pub fn get_u8_into(self) -> Result<Vec<u8>, DataVector>
pub fn get_i16_into(self) -> Result<Vec<i16>, DataVector>
pub fn get_i32_into(self) -> Result<Vec<i32>, DataVector>
pub fn get_f32_into(self) -> Result<Vec<f32>, DataVector>
pub fn get_f64_into(self) -> Result<Vec<f64>, DataVector>
Trait Implementations§
Source§impl Clone for DataVector
impl Clone for DataVector
Source§fn clone(&self) -> DataVector
fn clone(&self) -> DataVector
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for DataVector
impl Debug for DataVector
Source§impl PartialEq for DataVector
impl PartialEq for DataVector
impl StructuralPartialEq for DataVector
Auto Trait Implementations§
impl Freeze for DataVector
impl RefUnwindSafe for DataVector
impl Send for DataVector
impl Sync for DataVector
impl Unpin for DataVector
impl UnwindSafe for DataVector
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more