logoalt Hacker News

lenkite • yesterday at 7:56 PM • 1 reply • view on HN

> There may very well be libraries out there whose APIs expect you to pass Vecs into them, but that would be highly atypical.

    arrow       Buffer::from_vec(Vec<T>)
    ndarray     Array::from_vec(Vec<T>)
    ndarray     Array::from_shape_vec(shape, Vec<T>)
    bytes       Bytes::from(Vec<u8>)
    reqwest     Body::from(Vec<u8>)
    serde_json  Value::Array(Vec<Value>)
 
Dozens of example of very popular rust libraries. Usage is regular, not atypical. The slice variants generally require allocating and copying data or produces a borrowed object. Ownership transfer without copying is a problem.

allocator-aware APIs can enable zero-copy ownership transfer across library boundaries, even when the memory was allocated using a custom allocator. This is wowza power.


Replies

tialaramex • yesterday at 9:51 PM

That's six, which is hardly dozens. All of the examples you gave aren't allocator aware. They will reject a Vec<T,Goose> unless Goose is actually the Global allocator. Even if Goose is equivalent by Rust's allocator rules that's not good enough, the type matching wants an exact match not just allocator equivalence.

And this makes some sense because Vec<T,Global> has properties they may care about since they're the new owners of this Vec, they don't want a Vec<T,Goose> which might have very different ideas about allocation.

As your parent says, most APIs care about slices and thus do not communicate an allocator, so they're agnostic to allocation.