Giter Club home page Giter Club logo

rejoin_slice's Introduction

rejoin_slice

This crate provides functions to join two slices that are adjacent in memory. It is useful for rejoining slices that are split from the same slice, but need to be processed as a continous slice later:

let mut values: Vec<_> = util_lib::split_by_streak("aaaaaaabbbbbbbcccccccddddeeeeeeefffggggggggh");
let last_two = &values[values.len()-2].rejoin(&values[values.len()-1]);
assert_eq!(&"ggggggggh", last_two);

Notes about safety.

The API provided by this crate is not sound; I've yanked the published versions. I wrote this crate as a sample/experiment for a Rust RFC that was discussed at the time: rust-lang/rfcs#2806 Please refer to that discussion for why it isn't sound.

(Old, misguided) Notes about safety

This crate internally uses unsafe to achieve its functionality. However, it provides a safe interface. It takes the following precautions for safety:

  1. Pointer arithmetic is never explicitly performed. A pointer pointing to the end of the first slice is calculated using safe API's.
  2. Equality comparisons between pointers, although undefined behaviour in C in cases where the pointers originate from different objects, can be considered to be safe in Rust. This is ensured by the fact that the standard library provides a safe function std::ptr::eq to compares pointers.
  3. unsafe is only used to call std::slice::from_raw_parts to create a new slice after the check that the input slices are adjacent in memory.

rejoin_slice's People

Contributors

golddranks avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

rejoin_slice's Issues

This crate is unsoud

Despite the "Notes about safety" section in the readme, this crate is unsound because it allows creating a slice of different allocated objects. See this example of incorrect usage of core::slice::from_raw_parts in it's docs:

use std::slice;

fn join_slices<'a, T>(fst: &'a [T], snd: &'a [T]) -> &'a [T] {
    let fst_end = fst.as_ptr().wrapping_add(fst.len());
    let snd_start = snd.as_ptr();
    assert_eq!(fst_end, snd_start, "Slices must be contiguous!");
    unsafe {
        // The assertion above ensures `fst` and `snd` are contiguous, but they might
        // still be contained within _different allocated objects_, in which case
        // creating this slice is undefined behavior.
        slice::from_raw_parts(fst.as_ptr(), fst.len() + snd.len())
    }
}

fn main() {
    // `a` and `b` are different allocated objects...
    let a = 42;
    let b = 27;
    // ... which may nevertheless be laid out contiguously in memory: | a | b |
    let _ = join_slices(slice::from_ref(&a), slice::from_ref(&b)); // UB
}

Consider yanking all crate versions.

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.