Opened 16 months ago

Last modified 6 months ago

#25504 new enhancement

Find more generic ways to handle smartlist_t/Vec<T> between C and Rust

Reported by: isis Owned by:
Priority: Medium Milestone: Tor: unspecified
Component: Core Tor/Tor Version:
Severity: Normal Keywords: rust, datatypes, ffi, 035-removed-20180711
Cc: chelseakomlo, isis, catalyst Actual Points:
Parent ID: Points: 3
Reviewer: Sponsor:

Description

From #25368, we discussed having a possibly more generic and/or more rusty way to handle our smartlist_ts in C (and whatever underlying types the smartlist contains). Right now we have a Stringlist type in src/rust/smartlist/smartlist.rs, which is a Rust representation of smartlist_t using C types, and then we have a conversion between that and a Vec<String>:

pub trait Smartlist<T> {
    fn get_list(&self) -> Vec<T>;
}

#[repr(C)]
pub struct Stringlist {
    pub list: *const *const c_char,
    pub num_used: c_int,
    pub capacity: c_int,
}

impl Smartlist<String> for Stringlist {
    fn get_list(&self) -> Vec<String> {
        // [...]
    }
}

I have not thought about this nearly as much as komlo has, but maybe one way to do it is to have direct conversion between a smartlist_t and a Vec<T>, where T is probably an opaque pointer to whatever type in C, or T is only allowed to be a String which we've copied from a non-NULL char* (e.g. impl From<Stringlist> for Vec<String>, or something, and then keep Stringlist private since internally it's a bunch of C types that we don't want propagating into our more Rusty code).

Another idea might be to only handle Vec<T>-like things in Rust (if/when we move to the Rust-is-required phase), since we already have a nice datatype there, and then provide safe interfaces for C code to do all the things with/to the vectors that it currently does. (This sounds easier and more maintainable to me.)

We should probably brainstorm other ideas of how we're going to do this generically moving forward, because our C code uses smartlists everywhere.

Child Tickets

Change History (4)

comment:1 Changed 16 months ago by catalyst

Cc: catalyst added

comment:2 Changed 14 months ago by chelseakomlo

Cc: chelseakomlo added; komlo removed

comment:3 Changed 13 months ago by nickm

Keywords: 035-removed-20180711 added
Milestone: Tor: 0.3.5.x-finalTor: unspecified

These tickets are being triaged out of 0.3.5. The ones marked "035-roadmap-proposed" may return.

comment:4 Changed 6 months ago by gaba

Sponsor: Sponsor8-can
Note: See TracTickets for help on using tickets.