How to wrap an Iterator that has references

The MappedRows iterator for rusqlite returns a rusqlite::Result on each iteration, but I would like to map that to a custom error so as not to expose rusqlite in the public api.

The problem is that I don't know how to wrap that iterator and then return it from a function. The iterator in question references a Statement , which in turn references a Connection . I thought I could store the Statement along with the MappedRows in a struct and return that, but I'm having no luck.

Does anyone know of the correct approach for this?

The reason this is very difficult is that if you move the struc, the references in the other field is invalidated. I recommend finding another solution.