[rust-dev] Cloning a statically-sized array

Ashish Myles marcianx at gmail.com
Sun Jan 19 09:32:57 PST 2014


Thanks! I was half thinking of implementing this. :)


On Sun, Jan 19, 2014 at 1:04 AM, Jason Fager <jfager at gmail.com> wrote:

> The workaround for this is a wrapper struct around the fixed-size vec,
> with the trait methods you want impl'd on the wrapper.
>
> I've got a macro for this:
>
> https://github.com/jfager/d3cap/blob/master/fixed_vec_macros.rs
>
> So to wrap a [u16,..8], you'd say something like:
>
> fixed_vec!(WrapperType, u16, 8)
>
> and get a back a type called WrapperType implementing IterBytes, Eq, Ord,
> and Clone (would be easy to add others).  Not ideal, but useful until this
> gets straightened out.
>
>
>
>
>
> On Sun, Jan 19, 2014 at 12:19 AM, Daniel Micay <danielmicay at gmail.com>wrote:
>
>> On Sun, Jan 19, 2014 at 12:17 AM, Ashish Myles <marcianx at gmail.com>
>> wrote:
>> > Now, I already know that statically-sized arrays of primitives are
>> > implicitly copyable, but consider, for example, a statically-sized
>> array of
>> > a non-copyable but Clone-able type.  I find that for v of type [T, ..2],
>> > v.clone() is not a static array.  Perhaps it's because v is being
>> implicitly
>> > treated as &[T] instead.
>> >
>> > Eg.
>> > --------------------------------------------------
>> > fn make_clone<T : Clone>(a : &T) -> T {
>> >     a.clone();
>> > }
>> >
>> > fn main() {
>> >     let a : [int, ..2] = [1, 2];
>> >     // error: failed to find an implementation of trait
>> std::clone::Clone
>> >     // for [int, .. 2]
>> >     make_clone(&a);
>> >
>> >     let a : [int, ..2] = [1, 2];
>> >     // error: mismatched types: expected `[int, .. 2]` but found
>> `&[int]`
>> >     // ([] storage differs: expected 2 but found &)
>> >     let b : [int, ..2] = a.clone();
>> > }
>> > --------------------------------------------------
>> >
>> > So is it a missing feature of rust that Clone is not supported
>> generated for
>> > statically-sized arrays or is there a more fundamental reason that it
>> > doesn't exist?
>> >
>> > Ashish
>>
>> Clone is entirely a library feature, and Rust currently provides no
>> way to implement methods on fixed-size arrays. The latter is the real
>> issue, because there are other methods like `Eq` that should be
>> implemented on them too.
>> _______________________________________________
>> Rust-dev mailing list
>> Rust-dev at mozilla.org
>> https://mail.mozilla.org/listinfo/rust-dev
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20140119/ad53fd13/attachment.html>


More information about the Rust-dev mailing list