Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern crate num;
- use self::num::FromPrimitive;
- use self::num::One;
- use self::num::traits::PrimInt;
- struct TestStruct<T> {
- val: T
- }
- impl<T: PrimInt> TestStruct<T> {
- pub fn new(val: T) -> TestStruct<T> { TestStruct { val: val} }
- // This fails. Error is:
- // error: the trait `num::traits::FromPrimitive` is not implemented for the type `T`
- pub fn add_one(&self) -> T { self.val + FromPrimitive::from_i8(1).unwrap() }
- // This does work:
- // pub fn add_one(&self) -> T { self.val + One::one() }
- }
- mod test {
- use super::TestStruct;
- #[test]
- fn test_foo() {
- let test_struct = TestStruct::new(1);
- assert_eq!(2, test_struct.add_one());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement