MCPcopy Create free account
hub / github.com/That3Percent/second-stack / acquire

Function acquire

src/lib.rs:267–284  ·  view source on GitHub ↗

TODO: Relax the TrustedLen constraint, opting instead to allocate the larger bound, and release any extra unused space after running the iterator.

(i: I)

Source from the content-addressed store, hash-verified

265// TODO: Relax the TrustedLen constraint, opting instead to allocate the larger bound,
266// and release any extra unused space after running the iterator.
267pub 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)]
287mod tests {

Callers 4

memory_is_reusedFunction · 0.85
dropsFunction · 0.85
empty_slice_okFunction · 0.85
zst_okFunction · 0.85

Calls 1

acquireMethod · 0.80

Tested by 4

memory_is_reusedFunction · 0.68
dropsFunction · 0.68
empty_slice_okFunction · 0.68
zst_okFunction · 0.68