Skip to content
This repository was archived by the owner on Jul 10, 2023. It is now read-only.

Commit 09e2d6d

Browse files
author
bors-servo
committed
Auto merge of #52 - nox:boxed-slice, r=SimonSapin
Implement HeapSizeOf for boxed slices <!-- Reviewable:start --> [<img src="https://reviewable.io/review_button.svg" height="40" alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/heapsize/52) <!-- Reviewable:end -->
2 parents 3b8141a + 2a64a77 commit 09e2d6d

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/lib.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ pub trait HeapSizeOf {
8888
//
8989
// However, in the best case, the two approaches should give the same results.
9090
//
91-
impl<T: HeapSizeOf> HeapSizeOf for Box<T> {
91+
impl<T: HeapSizeOf + ?Sized> HeapSizeOf for Box<T> {
9292
fn heap_size_of_children(&self) -> usize {
9393
// Measure size of `self`.
9494
unsafe {
@@ -97,6 +97,12 @@ impl<T: HeapSizeOf> HeapSizeOf for Box<T> {
9797
}
9898
}
9999

100+
impl<T: HeapSizeOf> HeapSizeOf for [T] {
101+
fn heap_size_of_children(&self) -> usize {
102+
self.iter().fold(0, |size, item| size + item.heap_size_of_children())
103+
}
104+
}
105+
100106
impl HeapSizeOf for String {
101107
fn heap_size_of_children(&self) -> usize {
102108
unsafe {
@@ -276,7 +282,7 @@ macro_rules! known_heap_size(
276282
);
277283
);
278284

279-
known_heap_size!(0, char);
285+
known_heap_size!(0, char, str);
280286
known_heap_size!(0, u8, u16, u32, u64, usize);
281287
known_heap_size!(0, i8, i16, i32, i64, isize);
282288
known_heap_size!(0, bool, f32, f64);

tests/tests.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub const EMPTY: *mut () = 0x1 as *mut ();
1515
mod unstable {
1616
extern crate alloc;
1717

18-
use heapsize::heap_size_of;
18+
use heapsize::{HeapSizeOf, heap_size_of};
1919
use std::os::raw::c_void;
2020

2121
#[repr(C, simd)]
@@ -91,6 +91,12 @@ mod unstable {
9191
let x = Box::new(OverAligned(0, 0, 0, 0));
9292
assert_eq!(unsafe { heap_size_of(&*x as *const _ as *const c_void) }, 32 + 32);
9393
}
94+
95+
#[test]
96+
fn test_boxed_str() {
97+
let x = "raclette".to_owned().into_boxed_str();
98+
assert_eq!(x.heap_size_of_children(), 8);
99+
}
94100
}
95101

96102
#[test]
@@ -157,3 +163,9 @@ fn test_heap_size() {
157163
let x = vec![0i64, 1i64, 2i64, 3i64];
158164
assert_eq!(x.heap_size_of_children(), 32);
159165
}
166+
167+
#[test]
168+
fn test_boxed_slice() {
169+
let x = vec![1i64, 2i64].into_boxed_slice();
170+
assert_eq!(x.heap_size_of_children(), 16)
171+
}

0 commit comments

Comments
 (0)