Advertisement
Guest User

Untitled

a guest
May 25th, 2015
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. extern crate num;
  2.  
  3. use self::num::FromPrimitive;
  4. use self::num::One;
  5. use self::num::traits::PrimInt;
  6.  
  7. struct TestStruct<T> {
  8. val: T
  9. }
  10.  
  11. impl<T: PrimInt> TestStruct<T> {
  12. pub fn new(val: T) -> TestStruct<T> { TestStruct { val: val} }
  13.  
  14. // This fails. Error is:
  15. // error: the trait `num::traits::FromPrimitive` is not implemented for the type `T`
  16. pub fn add_one(&self) -> T { self.val + FromPrimitive::from_i8(1).unwrap() }
  17.  
  18. // This does work:
  19. // pub fn add_one(&self) -> T { self.val + One::one() }
  20. }
  21.  
  22. mod test {
  23. use super::TestStruct;
  24.  
  25. #[test]
  26. fn test_foo() {
  27. let test_struct = TestStruct::new(1);
  28. assert_eq!(2, test_struct.add_one());
  29. }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement