Giter Site home page Giter Site logo

type_erased_vec's Introduction

type_erased_vec

Crate Crates.io API

This crate provides a single struct TypeErasedVec, which (as its name says) is a type erased Vec.

When you know what its type is, you can get a slice or Vec back using TypeErasedVec::get or TypeErasedVec::get_mut.

Motivation

When communicating with a world outside Rust (GPU for example), it often wants a raw buffer and some kind of type descriptor.

There were two options for expressing this in Rust:

  • Vec<u8> + type descriptor.
  • Vec<T>.

The first option is not attractive because Vec<u8> cannot be safely used as Vec<T>, hence we lose the ability of modifying the buffer.

The second option makes all types holding that buffer generic over T, which is not feasible when T must be determined at runtime. For example, buffers can be loaded from a 3D model file on disk, where the file contains type information to be passed to the 3D renderer.

Example

use type_erased_vec::TypeErasedVec;

let mut vec = TypeErasedVec::new::<i32>();

{
    let mut vec_mut = unsafe { vec.get_mut() };
    for i in 0..10 {
        vec_mut.push(i);
    }
}

assert_eq!(unsafe { vec.get::<i32>() }, (0..10).collect::<Vec<_>>());

type_erased_vec's People

Contributors

chubei-oppen 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.