Christian Schmitt
Christian Schmitt

Reputation: 892

convert Vec<u32> to Vec<u64> (or Vec<std::os::raw::c_ulong>)

I'm trying to build a wrapper for a c library for windows and linux and have the problem that windows wants u32, while linux wants u64. currently my api always takes u32 know since it's easy to upcast to u64. However with Vec<32> I need to convert it via:

let unsafe_pages: Vec<bindings::size_t> = pages.iter().map(|&p| p as bindings::size_t).collect();

pages is a Vec<32>

is there an easier way to make the conversion without itering over the collection?

Upvotes: 0

Views: 931

Answers (1)

Sven Marnach
Sven Marnach

Reputation: 601499

No. You need to allocate twice the space on the heap to hold the new data, and you need to copy it there with twice the spacing. This requires iteration. Of course there could theoretically be a library function doing the iteration for you, but there isn't. Moreover, your code is already rather succinct, so there isn't much to improve. If you want to be slightly more succinct, you can replace either of the two occurrences of bindings::size_t with _.

Upvotes: 2

Related Questions