You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Jul 10, 2023. It is now read-only.
In a project I found myself needing to calculate the memory used by a PathBuf and ended up just ignoring the field. It'd be better if PathBuf implemented HeapSizeOf though.
I did a little spelunking through the std source code and it looks like PathBuf is just a newtype'd OsString, which is in turn just a newtype'd std::sys::Buf (os-specific string buffer). I stopped trying to write a PR soon after that because I'd started doing mem::transmutes between newtypes and digging into std::sys's os-specific internals, which felt a bit too hacky.
I think you could get pretty far with something like this:
use std::path::PathBuf;use std::ffi::OsString;use std::mem;implHeapSizeOfforPathBuf{fnheap_size_of_children(&self) -> usize{// PathBuf is just a newtype'd OsStringlet os_str:&OsString = unsafe{ mem::transmute(self)};
os_str.heap_size_of_children()}}implHeapSizeOfforOsString{fnheap_size_of_children(&self) -> usize{// the easy case, should work 99% of time.ifletSome(s) = self.to_str(){returnunsafe{heap_size_of(s.as_ptr())};}// fall back to a sane default or alternate implementation
...
}}
The text was updated successfully, but these errors were encountered:
In a project I found myself needing to calculate the memory used by a
PathBuf
and ended up just ignoring the field. It'd be better ifPathBuf
implementedHeapSizeOf
though.I did a little spelunking through the
std
source code and it looks likePathBuf
is just a newtype'dOsString
, which is in turn just a newtype'dstd::sys::Buf
(os-specific string buffer). I stopped trying to write a PR soon after that because I'd started doingmem::transmute
s between newtypes and digging intostd::sys
's os-specific internals, which felt a bit too hacky.I think you could get pretty far with something like this:
The text was updated successfully, but these errors were encountered: