GuillaumeGomez / rust-GSL

A GSL (the GNU Scientific Library) binding for Rust
190 stars 46 forks source link

`Vector<F>` trait #147

Open Chris00 opened 5 months ago

Chris00 commented 5 months ago

Many GSL functions take a slice &[F] (with F being f32, f64), a stride, and a length (not always checked to be sound in the higher level bindings). What about having a trait such as

trait Vector<F> {
    fn len(&self) -> usize;
    fn stride(&self) -> usize;
    fn as_slice(&self) -> &[F];
    fn as_slice_mut(&mut self) -> &mut [F];
}

which could be implemented for various types (all that implement AsRef<[f64]> but maybe also (impl AsRef<[f64]>, usize) to specify a stride,...). The length would never be passed to the functions, restricting the slice would be the way to go.

GuillaumeGomez commented 5 months ago

This is a pretty good idea if it allows to use more types for the same APIs. :+1: