Giter Club home page Giter Club logo

collect_into_rc_slice's Introduction

collect_into_rc_slice

A crate that let's you collect an Iterator<Item=T> into an Rc<[T]> or Arc<[T]> without needing to make 2 heap allocations.

Important Note

Please DO NOT use this if you already have a Vec<T>, sString or &[T] that contains the exact block memory you are trying convert to Rc<[T]>.

It wouldn't do anything better than the std implementation. It always better to use .into() in this case.

For example

use std::rc::Rc;

let v = vec![1,2,3];
let rc: Rc<[i32]> = v.into(); // Just use .into()

The Problem

You just learned about how cool using Rc<[T]> can be and you have an Iterator<Item=char> and you want to collect it to Rc<str>

One could naively do it as:

use std::rc::Rc;

let iter = "Hello, world!".chars();
let rc: Rc<str>  = iter.collect::<String>().into();

Which makes 2 seperate heap allocations, one for String and another one for Rc

Solution

It is very possible to do this with only 1 heap allocation, however it requires the usage of unsafe code, and good knowledge of the internal data structure of the smart pointers

With this crate you can avoid another heap allocation:

use collect_into_rc_slice::*;
use std::rc::Rc;

let iter = "Hello, world!".chars(); // Some iterator
let rc: Rc<str>  = iter.collect_into_rc_str();

Safety

This crate utilizes unsafe code to create a safe abstraction. To ensure that it is safe, it is tested, and uses miri to identify possible undefined behavior

collect_into_rc_slice's People

Contributors

markos-th09 avatar

Stargazers

 avatar  avatar

Watchers

 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.