Skip to content

Commit be53d61

Browse files
author
Jorge Aparicio
committed
librustrt: use unboxed closures
1 parent b44b5da commit be53d61

File tree

2 files changed

+42
-17
lines changed

2 files changed

+42
-17
lines changed

src/librustrt/args.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ mod imp {
8989
})
9090
}
9191

92-
fn with_lock<T>(f: || -> T) -> T {
92+
fn with_lock<T, F>(f: F) -> T where F: FnOnce() -> T {
9393
unsafe {
9494
let _guard = LOCK.lock();
9595
f()

src/librustrt/c_str.rs

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ use collections::hash;
7272
use core::fmt;
7373
use core::kinds::{Sized, marker};
7474
use core::mem;
75+
use core::ops::{FnMut, FnOnce};
7576
use core::prelude::{Clone, Drop, Eq, Iterator};
7677
use core::prelude::{SlicePrelude, None, Option, Ordering, PartialEq};
7778
use core::prelude::{PartialOrd, RawPtr, Some, StrPrelude, range};
@@ -319,14 +320,18 @@ pub trait ToCStr for Sized? {
319320
///
320321
/// Panics the task if the receiver has an interior null.
321322
#[inline]
322-
fn with_c_str<T>(&self, f: |*const libc::c_char| -> T) -> T {
323+
fn with_c_str<T, F>(&self, f: F) -> T where
324+
F: FnOnce(*const libc::c_char) -> T,
325+
{
323326
let c_str = self.to_c_str();
324327
f(c_str.as_ptr())
325328
}
326329

327330
/// Unsafe variant of `with_c_str()` that doesn't check for nulls.
328331
#[inline]
329-
unsafe fn with_c_str_unchecked<T>(&self, f: |*const libc::c_char| -> T) -> T {
332+
unsafe fn with_c_str_unchecked<T, F>(&self, f: F) -> T where
333+
F: FnOnce(*const libc::c_char) -> T,
334+
{
330335
let c_str = self.to_c_str_unchecked();
331336
f(c_str.as_ptr())
332337
}
@@ -344,12 +349,16 @@ impl ToCStr for str {
344349
}
345350

346351
#[inline]
347-
fn with_c_str<T>(&self, f: |*const libc::c_char| -> T) -> T {
352+
fn with_c_str<T, F>(&self, f: F) -> T where
353+
F: FnOnce(*const libc::c_char) -> T,
354+
{
348355
self.as_bytes().with_c_str(f)
349356
}
350357

351358
#[inline]
352-
unsafe fn with_c_str_unchecked<T>(&self, f: |*const libc::c_char| -> T) -> T {
359+
unsafe fn with_c_str_unchecked<T, F>(&self, f: F) -> T where
360+
F: FnOnce(*const libc::c_char) -> T,
361+
{
353362
self.as_bytes().with_c_str_unchecked(f)
354363
}
355364
}
@@ -366,12 +375,16 @@ impl ToCStr for String {
366375
}
367376

368377
#[inline]
369-
fn with_c_str<T>(&self, f: |*const libc::c_char| -> T) -> T {
378+
fn with_c_str<T, F>(&self, f: F) -> T where
379+
F: FnOnce(*const libc::c_char) -> T,
380+
{
370381
self.as_bytes().with_c_str(f)
371382
}
372383

373384
#[inline]
374-
unsafe fn with_c_str_unchecked<T>(&self, f: |*const libc::c_char| -> T) -> T {
385+
unsafe fn with_c_str_unchecked<T, F>(&self, f: F) -> T where
386+
F: FnOnce(*const libc::c_char) -> T,
387+
{
375388
self.as_bytes().with_c_str_unchecked(f)
376389
}
377390
}
@@ -397,11 +410,15 @@ impl ToCStr for [u8] {
397410
CString::new(buf as *const libc::c_char, true)
398411
}
399412

400-
fn with_c_str<T>(&self, f: |*const libc::c_char| -> T) -> T {
413+
fn with_c_str<T, F>(&self, f: F) -> T where
414+
F: FnOnce(*const libc::c_char) -> T,
415+
{
401416
unsafe { with_c_str(self, true, f) }
402417
}
403418

404-
unsafe fn with_c_str_unchecked<T>(&self, f: |*const libc::c_char| -> T) -> T {
419+
unsafe fn with_c_str_unchecked<T, F>(&self, f: F) -> T where
420+
F: FnOnce(*const libc::c_char) -> T,
421+
{
405422
with_c_str(self, false, f)
406423
}
407424
}
@@ -418,19 +435,24 @@ impl<'a, Sized? T: ToCStr> ToCStr for &'a T {
418435
}
419436

420437
#[inline]
421-
fn with_c_str<T>(&self, f: |*const libc::c_char| -> T) -> T {
438+
fn with_c_str<T, F>(&self, f: F) -> T where
439+
F: FnOnce(*const libc::c_char) -> T,
440+
{
422441
(**self).with_c_str(f)
423442
}
424443

425444
#[inline]
426-
unsafe fn with_c_str_unchecked<T>(&self, f: |*const libc::c_char| -> T) -> T {
445+
unsafe fn with_c_str_unchecked<T, F>(&self, f: F) -> T where
446+
F: FnOnce(*const libc::c_char) -> T,
447+
{
427448
(**self).with_c_str_unchecked(f)
428449
}
429450
}
430451

431452
// Unsafe function that handles possibly copying the &[u8] into a stack array.
432-
unsafe fn with_c_str<T>(v: &[u8], checked: bool,
433-
f: |*const libc::c_char| -> T) -> T {
453+
unsafe fn with_c_str<T, F>(v: &[u8], checked: bool, f: F) -> T where
454+
F: FnOnce(*const libc::c_char) -> T,
455+
{
434456
let c_str = if v.len() < BUF_LEN {
435457
let mut buf: [u8, .. BUF_LEN] = mem::uninitialized();
436458
slice::bytes::copy_memory(&mut buf, v);
@@ -489,9 +511,12 @@ impl<'a> Iterator<libc::c_char> for CChars<'a> {
489511
///
490512
/// The specified closure is invoked with each string that
491513
/// is found, and the number of strings found is returned.
492-
pub unsafe fn from_c_multistring(buf: *const libc::c_char,
493-
count: Option<uint>,
494-
f: |&CString|) -> uint {
514+
pub unsafe fn from_c_multistring<F>(buf: *const libc::c_char,
515+
count: Option<uint>,
516+
mut f: F)
517+
-> uint where
518+
F: FnMut(&CString),
519+
{
495520

496521
let mut curr_ptr: uint = buf as uint;
497522
let mut ctr = 0;
@@ -678,7 +703,7 @@ mod tests {
678703

679704
#[test]
680705
fn test_clone_noleak() {
681-
fn foo(f: |c: &CString|) {
706+
fn foo<F>(f: F) where F: FnOnce(&CString) {
682707
let s = "test".to_string();
683708
let c = s.to_c_str();
684709
// give the closure a non-owned CString

0 commit comments

Comments
 (0)