Giter Site home page Giter Site logo

orxfun / orx-imp-vec Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 123 KB

A data structure to enable conveniently building performant self referential collections such as trees, graphs and linked lists.

Home Page: https://crates.io/crates/orx-imp-vec

Rust 100.00%
data-structures pinned pinned-elements rust self-referential self-referential-structure vector

orx-imp-vec's Introduction

orx-imp-vec

orx-imp-vec crate orx-imp-vec documentation

ImpVec, standing for immutable push vector ๐Ÿ‘ฟ, is a data structure which allows appending elements with a shared reference.

Specifically, it extends vector capabilities with the following two methods:

  • fn imp_push(&self, value: T)
  • fn imp_extend_from_slice(&self, slice: &[T])

Note that both of these methods can be called with &self rather than &mut self.

Motivation

Appending to a vector with a shared reference sounds unconventional, and it is. However, if we consider our vector as a bag of or a container of things rather than having a collective meaning; then, appending element or elements to the end of the vector:

  • does not mutate any of already added elements, and hence,
  • it is not different than creating a new element in the scope.

Safety

It is natural to expect that appending elements to a vector does not affect already added elements. However, this is usually not the case due to underlying memory management. For instance, std::vec::Vec may move already added elements to different memory locations to maintain the contagious layout of the vector.

PinnedVec prevents such implicit changes in memory locations. It guarantees that push and extend methods keep memory locations of already added elements intact. Therefore, it is perfectly safe to hold on to references of the vector while appending elements.

Consider the classical example that does not compile, which is often presented to highlight the safety guarantees of rust:

let mut vec = vec![0, 1, 2, 3];

let ref_to_first = &vec[0];
assert_eq!(ref_to_first, &0);

vec.push(4);

// does not compile due to the following reason:  cannot borrow `vec` as mutable because it is also borrowed as immutable
// assert_eq!(ref_to_first, &0);

This beloved feature of the borrow checker of rust is not required and used for imp_push and imp_extend_from_slice methods of ImpVec since these methods do not require a &mut self reference. Therefore, the following code compiles and runs perfectly safely.

use orx_imp_vec::prelude::*;

let mut vec = ImpVec::new();
vec.extend_from_slice(&[0, 1, 2, 3]);

let ref_to_first = &vec[0];
assert_eq!(ref_to_first, &0);

vec.imp_push(4);
assert_eq!(vec.len(), 5);

vec.imp_extend_from_slice(&[6, 7]);
assert_eq!(vec.len(), 7);

assert_eq!(ref_to_first, &0);

License

This library is licensed under MIT license. See LICENSE for details.

orx-imp-vec's People

Contributors

orxfun avatar ugur-arikan avatar

Watchers

 avatar

orx-imp-vec's Issues

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.