Skip to content

Add a native function to copy a slice from a vector #8

@gfusee

Description

@gfusee

Abstract

Manipulating data vectors is common in Move packages. A fundamental operation is the ability to retrieve a slice of a vector in the range of [start; end[.

A particular use case is when handling a vector of u8 (a.k.a bytes). Currently, a developer must copy the bytes one by one from the original vector to a new one to create the slice. This is far from efficient, as it involves performing a number of read, copy, and write operations equal to the length of the slice range.

Solution proposed

To address this, I propose implementing a new native function, public native fun vector::copy_slice<T: copy>(v: &vector<T>, start: u64, end: u64): vector<T>. Constraining T to have the copy ability is mandatory to ensure that no Move rule is violated when copying the data into a slice.

As a native function, it would take advantage of the Rust runtime to read the container, copy the values once, and return them as a vector<T>.

If you agree with this idea, I can work on it and submit a PR 🙏

(cc @damirka)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions