I am writing a Rust binding for C library. It implements an object that can be created from different source objects, possibly keeping some links inside. I want the Rust type to apply the secure ownership policy, so the wrapper structure is a generic, parameterized type of stored reference.
struct Foobar<T> { origin: T, }
Then I implement some constructors for my Foobar type.
impl<T> Foobar<T> { fn from_nowhere() -> Foobar<()> { Foobar { origin: () } } fn from_orange<F>(orange: &mut F) -> Foobar<&mut F> where F: Orange { Foobar { origin: orange } } fn from_callback<F>(callback: F) -> Foobar<F> where F: FnMut(u64) -> u64 { Foobar { origin: callback } } }
And here a problem arises: both the structure and the constructor are independently parameterized. Although a constructor type parameter can be inferred from its arguments, a struct parameter is not used in the constructor and cannot be inferred. So the naive way to call the constructor
let a = Foobar::from_nowhere(); let b = Foobar::from_orange(&mut fruit); let c = Foobar::from_callback(|x| x*x);
confuses rustc:
rustgen.rs:43:13: 43:33 error: unable to infer enough type information about `_`; type annotations required [E0282] rustgen.rs:43 let a = Foobar::from_nowhere();
It can be fixed by providing some parameter of an arbitrary type:
let a = Foobar::<()>::from_nowhere(); let b = Foobar::<()>::from_orange(&mut fruit); let c = Foobar::<()>::from_callback(|x| x*x);
... these are all kinds of ugly. Another way to solve the problem would be to turn the constructors into free functions, although this would be (inaccurate) non-idiomatic.
The question is, am I missing something? The design seems to be incorrect. What would be the right way to create this type to get away with the same generic level?
Minimal reproducible example in a rust arena
For reference, my compiler version:
$ rustc --version rustc 1.1.0-dev (built 2015-04-26)