Skip to content

Commit df95719

Browse files
committed
Add Clone impls for extern "C" and unsafe fns
We only implemented Clone on `extern "Rust" fn`s (for up to 8 parameters). This didn't cover `extern "C"` or `unsafe` (or `unsafe extern "C"`) `fn`s, but there's no reason why they shouldn't be cloneable as well. The new impls are marked unstable because the existing impl for `extern "Rust" fn`s is. Fixes #24161.
1 parent b41f2df commit df95719

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/libcore/clone.rs

+21
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,27 @@ macro_rules! extern_fn_clone {
9797
#[inline]
9898
fn clone(&self) -> extern "Rust" fn($($A),*) -> ReturnType { *self }
9999
}
100+
101+
#[unstable(feature = "core", reason = "brand new")]
102+
impl<$($A,)* ReturnType> Clone for extern "C" fn($($A),*) -> ReturnType {
103+
/// Return a copy of a function pointer
104+
#[inline]
105+
fn clone(&self) -> extern "C" fn($($A),*) -> ReturnType { *self }
106+
}
107+
108+
#[unstable(feature = "core", reason = "brand new")]
109+
impl<$($A,)* ReturnType> Clone for unsafe extern "Rust" fn($($A),*) -> ReturnType {
110+
/// Return a copy of a function pointer
111+
#[inline]
112+
fn clone(&self) -> unsafe extern "Rust" fn($($A),*) -> ReturnType { *self }
113+
}
114+
115+
#[unstable(feature = "core", reason = "brand new")]
116+
impl<$($A,)* ReturnType> Clone for unsafe extern "C" fn($($A),*) -> ReturnType {
117+
/// Return a copy of a function pointer
118+
#[inline]
119+
fn clone(&self) -> unsafe extern "C" fn($($A),*) -> ReturnType { *self }
120+
}
100121
)
101122
}
102123

src/test/run-pass/issue-24161.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#[derive(Copy,Clone)]
12+
struct Functions {
13+
a: fn(u32) -> u32,
14+
b: extern "C" fn(u32) -> u32,
15+
c: unsafe fn(u32) -> u32,
16+
d: unsafe extern "C" fn(u32) -> u32
17+
}
18+
19+
pub fn main() {}

0 commit comments

Comments
 (0)