mirror of
https://github.com/Noratrieb/vechonk.git
synced 2026-01-14 08:35:10 +01:00
layout
This commit is contained in:
parent
c107680b95
commit
0764c02fa5
2 changed files with 68 additions and 3 deletions
28
README.md
Normal file
28
README.md
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
A `Vec<T: ?Sized>`
|
||||
|
||||
It's implemented by laying out the elements in memory contiguously like `alloc::vec::Vec`
|
||||
|
||||
# Layout
|
||||
|
||||
A `Vechonk` is 3 `usize` long. It owns a single allocation, containing the elements and the metadata.
|
||||
The elements are laid out contiguously from the front, while the metadata is laid out contiguously from the back.
|
||||
Both grow towards the center until they meet and get realloced to separate them again.
|
||||
|
||||
```txt
|
||||
Vechonk<str>
|
||||
------------------------
|
||||
| ptr | len | cap |
|
||||
---|---------------------
|
||||
|
|
||||
|___
|
||||
|
|
||||
Heap v
|
||||
------------------------------------------------------------------------
|
||||
| "hello" | "uwu" | <uninit> | 0 - 5 | 5 - 3 |
|
||||
|-----------|----------|-----------------|--------------|--------------|
|
||||
| dynamic | dynamic | rest of alloc | usize + meta | usize + meta |
|
||||
--------------------------------------------|--------------|------------
|
||||
^ ^ | |
|
||||
|___________ | _________________________| |
|
||||
|_________________________________________|
|
||||
```
|
||||
43
src/lib.rs
43
src/lib.rs
|
|
@ -1,9 +1,46 @@
|
|||
#![no_std]
|
||||
#![feature(ptr_metadata)]
|
||||
#![feature(ptr_metadata, ptr_internals)]
|
||||
|
||||
//!
|
||||
//! A `Vec<T: ?Sized>`
|
||||
//!
|
||||
//! It's implemented by laying out the elements in memory contiguously like `alloc::vec::Vec`
|
||||
//! It's implemented by laying out the elements in memory contiguously like [`alloc::vec::Vec`]
|
||||
//!
|
||||
//! # Layout
|
||||
//!//!
|
||||
//! A [`Vechonk`] is 3 `usize` long. It owns a single allocation, containing the elements and the metadata.
|
||||
//! The elements are laid out contiguously from the front, while the metadata is laid out contiguously from the back.
|
||||
//! Both grow towards the center until they meet and get realloced to separate them again.
|
||||
//!
|
||||
//! ```txt
|
||||
//!
|
||||
//! Vechonk<str>
|
||||
//! ------------------------
|
||||
//! | ptr | len | cap |
|
||||
//! ---|---------------------
|
||||
//! |
|
||||
//! |___
|
||||
//! |
|
||||
//! Heap v
|
||||
//! ------------------------------------------------------------------------
|
||||
//! | "hello" | "uwu" | <uninit> | 0 - 5 | 5 - 3 |
|
||||
//! |-----------|----------|-----------------|--------------|--------------|
|
||||
//! | dynamic | dynamic | rest of alloc | usize + meta | usize + meta |
|
||||
//! --------------------------------------------|--------------|------------
|
||||
//! ^ ^ | |
|
||||
//! |___________ | _________________________| |
|
||||
//! |_________________________________________|
|
||||
//! ```
|
||||
|
||||
struct Vechonk<T: ?Sized>;
|
||||
extern crate alloc;
|
||||
|
||||
use core::marker::PhantomData;
|
||||
use core::ptr::{NonNull};
|
||||
use alloc::vec::Vec;
|
||||
|
||||
/// chonky af
|
||||
struct Vechonk<T: ?Sized> {
|
||||
size: usize,
|
||||
cap: usize,
|
||||
ptrs: Vec<NonNull<T>>
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue