Hello! I would like some help figuring out how to write this bit of code, if possible
I am designing an API where the presence of type substitution is denoted at the type level.
I have this generic container type:
struct Substituted<T> {
inner: T,
marker: bool,
}
and the function signatures in this API take this Substituted
type:
struct Data {
inner: u64
}
fn api_method(data: Substituted<Data>) -> Substituted<u64> {
// I want to put a map method here
// to map Substituted<T> -> Substituted<U>
//
// I want to do this:
// data.map(|d| d.inner)
}
I've attempted to model this method after the implementation for Option
, however for this API I cannot use nightly Rust, so I am running into some snags. Do y'all have any suggestions for how I might go around this?
Here's what I have so far:
impl<T> Substituted<T> {
pub const fn map<U, F>(self, f: F) -> Substituted<U>
where
F: FnOnce(T) -> U,
{
let Substituted { inner, marker } = self;
Substituted::new(f(inner), marker)
}
}
I'm getting this error:
the trait bound `F: std::ops::FnOnce<(T,)>` is not satisfied
expected an `FnOnce<(T,)>` closure, found `F`rustcClick for full compiler diagnostic
substituted.rs(52, 26): the trait `std::ops::FnOnce<(T,)>` is implemented for `F`, but that implementation is not `const`
substituted.rs(49, 26): consider further restricting this bound: ` + ~const std::ops::FnOnce<(T,)>`