I've been working on an emulator for awhile now and am very close to finishing the cpu part and am having a bit of trouble with a cryptic lifetime error.
Background -
This is a 68k cpu emulator based on r68k and Musashi. r68k had everything pretty much done just extracted everything and refactored some stuff to look cleaner. Main change is I pass a Bus trait everywhere so the user (myself) only has to implement the Bus trait (should be like a memory map read/write).
My error is when trying to add the opcode functions as pointers into the 64k opcode table I'm getting "expected concrete lifetime, found bound lifetime parameter" on all my opcode additions.
I know I'm using the feature 'universal_impl_trait', I feel that makes the code look cleaner. I did try to add explicit lifetimes everywhere, but still got the same error, so I don't think it's that.
Before I throw in the towel on this Trait Object as an interface I'd love to hear any help or suggestions. My only alternative is callbacks? or forget the separate crate and just hard code the cpu in. My hope was for something others could use again.
It would be helpful if you'd include the relevant variables and types that result in the error in your post so as to avoid having to look through thousands of lines of code.
I'm not sure why you can't get a higher-kinded lifetime version of foo. One way to fix it is to remove the higher kindedness and add an explicit lifetime parameter to your Handler type:
fn foo<T: AsMut<u8> + ?Sized>(_: &mut T) {}
type FnVec<'a> = Vec<fn(&mut (AsMut<u8> + 'a))>;
fn main() {
let mut bar: FnVec = vec![];
bar.push(foo);
}
Alternatively, if you're going to be using trait objects, just define all your handlers to take a trait object as well? (Instead of a generic)
That looks like an impossible bound - it’s saying the caller picks the lifetime of the reference but callee picks the lifetime the trait object is valid over.
I filled out all the types and made sure I could call the function pointer and impl the Bus. I will keep messing around with this and keep you updated!
I've tried to minimize code so it's easy to follow, but still represents my actual use case.
Notice how I can call the function pointer no problem outside of the impl (line 52) but when I try to call it in my step function (uncomment line 18) I get the same T doesn't have a constant size at compile time. I'm lost on this. Any help is much appreciated.
Since you’re turning the bus generic type param into a trait object, you need to drop the ?Sized bound on step. In addition, you need to indicate that the object is valid over lifetime 'a: Rust Playground