Reputation: 15809
I'm trying to use a third-party library in Rust that accepts only &[u8] as input. The actual input I'd like to supply exists as a bunch of separate arrays of u8.
Is it possible to create something that looks like a &[u8] but actually fetches the data from my underlying &[u8]s?
I'm new to Rust and may be having some conceptual difficulties, but this looks like an interface to me (https://doc.rust-lang.org/std/primitive.slice.html), and in other languages I can implement interfaces using arbitrary code. Can I do it in this case?
Upvotes: 0
Views: 807
Reputation: 225105
A slice type [T]
for some T
is a concrete type, not an interface/trait, and a slice has to be a contiguous region of memory. You can concatenate slices into some container by copying their contents:
let a: &[u8] = &[1, 2, 3];
let b: &[u8] = &[4, 5];
foo(&[a, b].concat())
If you can’t do that, barring memory mapping hacks (not accessible to safe Rust), the library would need to be changed to accept something more general.
Upvotes: 5