http icon indicating copy to clipboard operation
http copied to clipboard

Api to consume Extensions and import/export all items

Open soundofspace opened this issue 3 months ago • 1 comments

When traversing boundaries it's currently quite hard (nearly impossible) to extract all extensions and transfer them to somewhere else. There are some creative ways to work around this, like inserting a nested typemap into extensions, but these are all quite hacky.

Would you be open into some sort of api that can consume extensions and return the individual items, and then later add these back to a new or existing Extension object?

A very quick solution could be something like

impl IntoIterator for Extensions {
    type Item = (TypeId, Box<dyn AnyClone + Send + Sync>);
    type IntoIter = std::vec::IntoIter<Self::Item>;

    fn into_iter(self) -> Self::IntoIter {
        match self.map {
            None => Default::default(),
            Some(map) => {
                map.into_iter().collect::<Vec<_>>().into_iter()
            }
        }
        
    }
}

But it could be anything else, only requirement would be the functionality to export all items from extensions and import them back again. The example implementation also hides away the usage of a hashmap so this detail could be changed in the future if needed. But it does require AnyClone to be public (can be sealed). Please don't focus on this solution too much as I haven't really given it much thought, it's just here as an example / starting point.

I'm really open to all suggestions here on how to support this or what other solutions there might be available. Having extensions be interoperable with other type maps would be really useful and would make extensions much more flexible.

This is slightly related to https://github.com/hyperium/http/issues/299

soundofspace avatar Sep 04 '25 12:09 soundofspace

Would you be open for something like this @seanmonstar ? This allows us to convert between extension types.

The other option could be where we introduce a new hyperium/extensions crate so that non-http use cases can also make use of extensions without having to import the http crate for that. And this would also make the need for such import/export features less needed in case you do not want that.

GlenDC avatar Sep 04 '25 12:09 GlenDC