Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created July 13, 2023 19:31
Show Gist options
  • Save rust-play/1352e3891e15756710d68c834da0a2b9 to your computer and use it in GitHub Desktop.
Save rust-play/1352e3891e15756710d68c834da0a2b9 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
trait HeapSize {
fn heap_size(&self) -> usize;
}
pub struct KnownElementHeapSize<T>(pub T);
trait KnownHeapSize {
const S: usize;
}
impl KnownHeapSize for u8 {
const S: usize = 0;
}
impl <E: KnownHeapSize> HeapSize for KnownElementHeapSize<Vec<E>> {
fn heap_size(&self) -> usize {
self.0.capacity() * std::mem::size_of::<E>() + self.0.len() * E::S
}
}
fn main() {
println!("Hello, world!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment