Crates.io | cable |
lib.rs | cable |
version | 0.1.1 |
source | src |
created_at | 2021-05-07 21:10:32.624215 |
updated_at | 2021-05-08 13:44:39.159214 |
description | A cable(pointer) with a hook(header at address) at the end and a sized payload(array) |
homepage | |
repository | https://github.com/Volhorc/cable |
max_upload_size | |
id | 394497 |
size | 23,854 |
A cable
(pointer) with a hook
(header at address) at the end and a sized payload
(array)
A pointer type for heap allocation, with some special features:
Add this to your Cargo.toml
:
[dependencies]
cable = "0.1.1"
let mut data: Cable<f64, (i32, i32, i32, i32)> = Cable::with_capacity_zeroed(8, (1, 2, 3, 4));
data[0] = 1.0;
data[1] = 6.0;
data[2] = 9.0;
for i in data.iter() {
println!("{:?}", i);
}
println!("Header: {:?}", data.header().unwrap());
println!("Footprint: {}", data.footprint());
The Cable<T, H>
is useful in creating other heap objects.
Creating a simple dynamic storage with a length and capacity:
let mut data: Cable<i32, usize> = Cable::with_capacity(24, 6); // allocate capacity for 24 elements
data[0] = 19;
data[1] = 22;
data[2] = 35;
data[3] = 53;
data[4] = 68;
data[5] = 13;
println!("Length: {}", data.header().unwrap());
println!("Footprint: {}", data.footprint());
The Cable<T, H>
works well for nested structures when a small footprint is required:
let mut x: Vec<Cable<i32>> = Vec::with_capacity(24);
x.push(Cable::with_capacity(2));
x[0][0] = 67;
x[0][1] = 45;
x.push(Cable::with_capacity(8));
x[1][2] = 32;
x[1][5] = 19;
In this case the vector acts like a 2D array but each element can have a variable size. This allows for compact data structures with proper bounds checking and a minimal footprint. A struct can be used as a header for convenience:
struct Info {
id: i32,
position: (f32, f32),
length: usize,
}
let mut x: Cable<i32, Info> = Cable::with_capacity(
24,
Info {
id: -1,
position: (0.0, 0.0),
length: 0,
},
);
A header may be omitted for brevity:
let mut x: Cable<i32> = Cable::new();
This pointer is safe as it always allocates at least mem::size_of::<usize>()
bytes on the heap and will point to that allocation.
A cable has some special allocation features and considerations:
mem::size_of::<H>()
+ padding for usize
+ mem::size_of::<usize>()
+ padding for T
.H = ()
the header is not allocated.Box<H>
when payload is unallocated (although with at least an extra mem::size_of::<usize>()
bytes, see into_boxed_header
).To be determined, will likely support in the future: