[rust-dev] Drop and lifetimes

David Brown davidb at davidb.org
Tue Jul 1 07:35:08 PDT 2014


Imagine a hypothetical database interface:

    struct Database { ... }
    struct Cursor<'a> {
        db: &'a Database,
        ...
    }
     
    impl Database {
        fn query<'a>(&'a self, ...) -> Cursor<'a> { ... }
    }

Ideally, I would like both the Database, and the Cursor to implement
Drop.  The underlying database API requires the cursors to all be
closed before the database itself is closed.

My first concern is that the trend seems to be to disallow Drop on the
Cursor (it requires #[unsafe_destructor] to compile right now).

My second concern is around what seems to be to be a reasonable use of
this:

    struct Thing<'a> {
        db: Database,
	q1: Cursor<'a>,
	...
    }

This would be common with the use of an SQL database, where there are
a handful of prepared statements that get reused with different data.
(minor other question, I can't figure out how to make a 'Thing',
everything I've tried results in some kind of lifetime error).

But, the question then becomes, what order would Drop be called when
Thing goes out of scope?

Am I missing a better way of doing all of this?  I don't see 'Rc' or
'Gc' helping, least because they only handle an immutable value, but
also, they are non-Send, so can't have Drop either.

Thanks,
David Brown


More information about the Rust-dev mailing list