Skip to content

Instantly share code, notes, and snippets.

@gavinb
Last active August 29, 2015 13:57
Show Gist options
  • Save gavinb/9924137 to your computer and use it in GitHub Desktop.
Save gavinb/9924137 to your computer and use it in GitHub Desktop.
Trying template types
use std::num::{Zero};
use std::vec::Vec;
use std::mem::size_of;
pub struct Image<T> {
width: uint,
height: uint,
// ...
buffer: Vec<T>,
}
impl<T: Zero, Clone> Image<T> {
fn stride_for_type(w: uint, ch: uint) -> uint {
let sz = size_of::<T>();
//...
3*w*sz
}
fn new(w: uint, h: uint) -> Image<T> {
let s = Image::<T>::stride_for_type(w, 3);
Image {
width: w,
height: h,
buffer: Vec::from_elem(s*h, Zero::zero()),
}
}
}
@gavinb
Copy link
Author

gavinb commented Apr 1, 2014

The error message:

$ rustc --crate-type lib templt1.rs 
templt1.rs:22:17: 22:44 error: the impl referenced by this path needs 2 type parameters, but 1 type parameter were supplied
templt1.rs:22         let s = Image::<T>::stride_for_type(w, 3);
                              ^~~~~~~~~~~~~~~~~~~~~~~~~~~
templt1.rs:22:17: 22:44 error: not enough type parameters provided: expected 2, found 1
templt1.rs:22         let s = Image::<T>::stride_for_type(w, 3);
                              ^~~~~~~~~~~~~~~~~~~~~~~~~~~
templt1.rs:22:17: 22:44 error: cannot determine a type for this bounded type parameter: unconstrained type
templt1.rs:22         let s = Image::<T>::stride_for_type(w, 3);
                              ^~~~~~~~~~~~~~~~~~~~~~~~~~~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment