TODO: Relax the TrustedLen constraint, opting instead to allocate the larger bound, and release any extra unused space after running the iterator.
(i: I)
| 265 | // TODO: Relax the TrustedLen constraint, opting instead to allocate the larger bound, |
| 266 | // and release any extra unused space after running the iterator. |
| 267 | pub fn acquire<T, I: Iterator<Item=T> + TrustedLen>(i: I) -> StackAlloc<T> { |
| 268 | THREAD_LOCAL_POOL.with(|rc| { |
| 269 | let len = i.size_hint(); |
| 270 | assert_eq!(Some(len.0), len.1); // Runtime check for ExactSizedIterator, since this isn't working for some obvious cases like repeat(v).take(5) |
| 271 | let pool = rc.borrow_mut().acquire(len.0); |
| 272 | let mut p = pool.ptr; |
| 273 | // TODO: To adhere to the standards of the nomicon, this needs to handle drop correctly in |
| 274 | // the face of a panic. Probably the thing to do is to have acquire just return a pointer tuple, and then |
| 275 | // wrap it up in the drop structure at the end. |
| 276 | for item in i { |
| 277 | unsafe { |
| 278 | ptr::write(p, item); |
| 279 | p = p.add(1); |
| 280 | } |
| 281 | } |
| 282 | pool |
| 283 | }) |
| 284 | } |
| 285 | |
| 286 | #[cfg(test)] |
| 287 | mod tests { |