我在给别人的泛型 API 写包装,它的 FromEncoding trait 支持大多数基本类型[1] (#footnote-682731-1)。我想让这个包装能够把该 API 提供的值收集到一个集合里。
一开始我想,“集合实现了 FromIterator<Element>,直接用它不就行了吗?”看来不行,因为 Rust 无法确定我指的是哪个 Element,这也很合理,多个 impl FromEncoding for IterElement 都可能对应一个 impl FromIterator<IterElement> for Bucket<BucketElement>。有办法处理这种情况吗,还是说应该只支持固定的几个集合,比如 Vec 和 HashSet?
如果这有助于排除 XY 问题,实际用到的 trait 是 rusqlite::FromSql。↩︎ (#footnote-ref-682731-1)
从英语翻译·查看原文I'm building a wrapper for somebody else's generic API, whose FromEncoding trait supports most primitive types[1] (#footnote-682731-1). I want this wrapper to support gathering the values supplied by that API together into a collection.
Initially I thought, "collections implement FromIterator<Element>, couldn't that handle it?" Apparently not, since Rust (quite reasonably) can't determine which Element I mean: several impl FromEncoding for IterElement could each match an impl FromIterator<IterElement> for Bucket<BucketElement>. Is there a way around this, or should I just support a fixed selection of collections (say Vec and HashSet)?
The actual trait here is rusqlite::FromSql, if that helps rule out an XY problem. ↩︎ (#footnote-ref-682731-1)