use std::convert::TryInto;
use crate::error::{SparseLinearAlgebraError, SystemError};
use crate::graphblas_bindings::GrB_Index;
pub type ElementIndex = usize;
pub type ElementCount = ElementIndex;
pub trait IndexConversion {
fn to_graphblas_index(&self) -> Result<GrB_Index, SparseLinearAlgebraError>;
fn as_graphblas_index(self) -> Result<GrB_Index, SparseLinearAlgebraError>;
fn from_graphblas_index(index: GrB_Index) -> Result<ElementIndex, SparseLinearAlgebraError>;
}
impl IndexConversion for ElementIndex {
fn to_graphblas_index(&self) -> Result<GrB_Index, SparseLinearAlgebraError> {
(*self).as_graphblas_index()
}
fn as_graphblas_index(self) -> Result<GrB_Index, SparseLinearAlgebraError> {
let graphblas_index: GrB_Index;
match self.try_into() {
Ok(as_graphblas_index) => graphblas_index = as_graphblas_index,
Err(error) => return Err(SystemError::from(error).into()),
}
Ok(graphblas_index)
}
fn from_graphblas_index(index: GrB_Index) -> Result<Self, SparseLinearAlgebraError> {
let element_index: ElementIndex;
match index.try_into() {
Ok(as_element_index) => element_index = as_element_index,
Err(error) => return Err(SystemError::from(error).into()),
}
Ok(element_index)
}
}