GithubHelp home page GithubHelp logo

michiel-de-muynck / moveslice Goto Github PK

View Code? Open in Web Editor NEW

This project forked from aejenk/moveslice

0.0 1.0 0.0 5 KB

A one-function utility crate for moving chunks of elements in a slice around, safely and quickly.

Rust 100.00%

moveslice's Introduction

moveslice

moveslice moveslice

This crate contains functionality to move a slice within an array around. It only uses safe functions, and acts efficiently by using the split_at_mut and rotate_left/rotate_right functions.

This crate also has a focus on being no_std, to allow this functionality in all case where it is required.

The main feature this crate provides is implementing moveslice functions for any and all slices/arrays. Note that this only works for slices - vectors and other structures would need to be converted into a slice before using this function.

Examples:

use moveslice::Moveslice;

let mut arr = [1,2,3,4,5,6,7,8,9];

// The following moves the slice 3..6 to index 1.
// In effect, it moves [4,5,6] over to where [2] is.
arr.moveslice(3..6, 1);
assert_eq!(arr, [1,4,5,6,2,3,7,8,9]);

// The following moves the slice 3..6 to index 6.
// In effect, it moves [6,2,3] over to where [7] is.
arr.moveslice(3..6, 6);
assert_eq!(arr, [1,4,5,7,8,9,6,2,3]);

// The following attempts to move the slice beyond boundaries.
// The index given is 7, which exists in the array, but the
// last element of the chunk will not fit (7 + 3 = 10 > 9).
// Therefore, the following should fail.
arr.moveslice(3..6, 7); // will panic

// Panicking on failure however can prove to be not ideal.
// If instead of panicking, you prefer a `Result`, use
// `try_moveslice`.
let res = arr.try_moveslice(3..6, 7);
assert!(res.is_err());

// Moveslice also comes with its own `Error` enum, to offer
// better debugging. Right now, there's only one error case.

// You could pass the destination as the same value as chunk.0.
// However this would mean nothing is moved.
// This doesn't panic, but it's a no-op.
arr.moveslice(0..3, 0);

License: MPL-2.0

moveslice's People

Contributors

aejenk avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.