Skip to content

Commit 494b713

Browse files
committed
Rename and move marker stuff
1 parent df0d754 commit 494b713

5 files changed

Lines changed: 119 additions & 88 deletions

File tree

src/base.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{
88
AsSafe, AsUnsafe, WithAbi,
99
abi::Abi,
1010
make_safe, make_unsafe,
11-
markers::{self, Safe, Unsafe},
11+
marker::{self, Safe, Unsafe},
1212
with_abi,
1313
};
1414

@@ -48,11 +48,11 @@ pub trait FnPtr:
4848
type Output;
4949

5050
/// Marker type denoting arity
51-
type ArityMarker: markers::Arity;
51+
type ArityMarker: marker::Arity;
5252
/// Marker type denoting safety
53-
type SafetyMarker: markers::Safety;
53+
type SafetyMarker: marker::Safety;
5454
/// Marker type denoting abi
55-
type AbiMarker: markers::Abi;
55+
type AbiMarker: marker::Abi;
5656

5757
/// The function's arity (number of arguments).
5858
const ARITY: usize;
@@ -110,7 +110,7 @@ pub trait FnPtr:
110110
/// # Safety
111111
/// Caller must ensure that the resulting ABI transformation is sound.
112112
#[must_use]
113-
unsafe fn with_abi<Abi: markers::Abi>(&self) -> with_abi!(Abi, Self)
113+
unsafe fn with_abi<Abi: marker::Abi>(&self) -> with_abi!(Abi, Self)
114114
where
115115
Self: WithAbi<Abi>,
116116
{
@@ -166,14 +166,14 @@ pub trait StaticFnPtr: FnPtr + 'static {}
166166
/// Marker trait implemented for function pointers of a specific ABI.
167167
///
168168
/// For example:
169-
/// - `HasAbi<markers::C>` for `extern "C" fn(...)`
170-
/// - `HasAbi<markers::Sysv64>` for `extern "sysv64" fn(...)`
171-
pub trait HasAbi<Abi: markers::Abi>: FnPtr {}
169+
/// - `HasAbi<marker::C>` for `extern "C" fn(...)`
170+
/// - `HasAbi<marker::Sysv64>` for `extern "sysv64" fn(...)`
171+
pub trait HasAbi<Abi: marker::Abi>: FnPtr {}
172172

173173
/// Marker trait denoting the safety of a function pointer type.
174174
///
175175
/// For example:
176176
/// - `HasSafety<Safe>` for `extern "C" fn(...)`
177177
/// - `HasSafety<Unsafe>` for `unsafe fn(...)`
178-
pub trait HasSafety<Safety: markers::Safety> {}
178+
pub trait HasSafety<Safety: marker::Safety> {}
179179
impl<F: FnPtr> HasSafety<F::SafetyMarker> for F {}

src/conversion.rs

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use crate::{
2-
FnPtr, HasAbi, HasSafety, markers::{self, Safe, Unsafe}
2+
FnPtr, HasAbi, HasSafety,
3+
marker::{self, Safe, Unsafe},
34
};
45

56
/// Helper trait to change the ABI of a function pointer type while preserving arity, safety, and signature.
67
pub trait WithAbi<Abi>: FnPtr
78
where
8-
Abi: markers::Abi,
9+
Abi: marker::Abi,
910
{
1011
/// The function pointer type with the requested ABI (preserving safety and signature).
1112
type F: FnPtr<
@@ -20,7 +21,7 @@ where
2021
/// Helper trait to change the safety of a function pointer type while preserving arity, ABI, and signature.
2122
pub trait WithSafety<Safety>: FnPtr
2223
where
23-
Safety: markers::Safety,
24+
Safety: marker::Safety,
2425
{
2526
/// The function pointer type with the requested safety (preserving ABI and signature).
2627
type F: FnPtr<
@@ -40,6 +41,38 @@ impl<F: WithSafety<Safe>> AsSafe for F {}
4041
pub trait AsUnsafe: WithSafety<Unsafe> {}
4142
impl<F: WithSafety<Unsafe>> AsUnsafe for F {}
4243

44+
/// Helper trait that simplifies generic bounds when converting between funciton pointer types.
45+
pub trait Convertible:
46+
FnPtr
47+
+ WithAbi<marker::Rust>
48+
+ WithAbi<marker::C>
49+
+ WithAbi<marker::CUnwind>
50+
+ WithAbi<marker::System>
51+
+ WithAbi<marker::SystemUnwind>
52+
+ WithAbi<marker::Rust>
53+
+ WithAbi<marker::Aapcs>
54+
+ WithAbi<marker::AapcsUnwind>
55+
+ WithAbi<marker::Cdecl>
56+
+ WithAbi<marker::CdeclUnwind>
57+
+ WithAbi<marker::Stdcall>
58+
+ WithAbi<marker::StdcallUnwind>
59+
+ WithAbi<marker::Fastcall>
60+
+ WithAbi<marker::FastcallUnwind>
61+
+ WithAbi<marker::Thiscall>
62+
+ WithAbi<marker::ThiscallUnwind>
63+
+ WithAbi<marker::Vectorcall>
64+
+ WithAbi<marker::VectorcallUnwind>
65+
+ WithAbi<marker::SysV64>
66+
+ WithAbi<marker::SysV64Unwind>
67+
+ WithAbi<marker::Win64>
68+
+ WithAbi<marker::Win64Unwind>
69+
+ WithSafety<marker::Safe>
70+
+ WithSafety<marker::Unsafe>
71+
+ AsSafe
72+
+ AsUnsafe
73+
{
74+
}
75+
4376
/// Construct a function-pointer type identical to the given one but using
4477
/// the specified ABI.
4578
///
@@ -50,10 +83,10 @@ impl<F: WithSafety<Unsafe>> AsUnsafe for F {}
5083
/// # Examples
5184
///
5285
/// ```rust
53-
/// # use fn_ptr::{with_abi, markers};
86+
/// # use fn_ptr::{with_abi, marker};
5487
/// type F = extern "C" fn(i32) -> i32;
5588
///
56-
/// type G = with_abi!(markers::SysV64, F);
89+
/// type G = with_abi!(marker::SysV64, F);
5790
/// // `G` is `extern "sysv64" fn(i32) -> i32`
5891
///
5992
/// type H = with_abi!("C", extern "system" fn());
@@ -68,7 +101,7 @@ macro_rules! with_abi {
68101

69102
// ABI given as a string literal
70103
( $abi_lit:tt, $ty:ty ) => {
71-
<$ty as $crate::WithAbi<$crate::markers::abi!($abi_lit)>>::F
104+
<$ty as $crate::WithAbi<$crate::abi!($abi_lit)>>::F
72105
};
73106
}
74107

@@ -86,7 +119,7 @@ macro_rules! with_abi {
86119
#[macro_export]
87120
macro_rules! make_safe {
88121
( $ty:ty ) => {
89-
<$ty as $crate::WithSafety<$crate::markers::Safe>>::F
122+
<$ty as $crate::WithSafety<$crate::marker::Safe>>::F
90123
};
91124
}
92125

@@ -104,6 +137,6 @@ macro_rules! make_safe {
104137
#[macro_export]
105138
macro_rules! make_unsafe {
106139
( $ty:ty ) => {
107-
<$ty as $crate::WithSafety<$crate::markers::Unsafe>>::F
140+
<$ty as $crate::WithSafety<$crate::marker::Unsafe>>::F
108141
};
109142
}

src/impl.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@ macro_rules! impl_fn {
8080
type Output = Ret;
8181

8282
type ArityMarker = impl_fn!(@arity_marker ($($ty),*));
83-
type SafetyMarker = $crate::markers::safety!($safety);
84-
type AbiMarker = $crate::markers::$abi_ident;
83+
type SafetyMarker = $crate::safety!($safety);
84+
type AbiMarker = $crate::marker::$abi_ident;
8585

86-
const ARITY: ::core::primitive::usize = <Self::ArityMarker as $crate::markers::Arity>::N;
87-
const IS_SAFE: ::core::primitive::bool = <Self::SafetyMarker as $crate::markers::Safety>::IS_SAFE;
88-
const ABI: $crate::Abi = <$crate::markers::$abi_ident as $crate::markers::Abi>::KIND;
86+
const ARITY: ::core::primitive::usize = <Self::ArityMarker as $crate::marker::Arity>::N;
87+
const IS_SAFE: ::core::primitive::bool = <Self::SafetyMarker as $crate::marker::Safety>::IS_SAFE;
88+
const ABI: $crate::Abi = <$crate::marker::$abi_ident as $crate::marker::Abi>::KIND;
8989
const IS_EXTERN: ::core::primitive::bool = !matches!(Self::ABI, $crate::Abi::Rust);
9090

9191
fn as_ptr(&self) -> $crate::UntypedFnPtr {
@@ -104,17 +104,17 @@ macro_rules! impl_fn {
104104

105105
// WithSafety
106106
#[automatically_derived]
107-
impl<Ret, $($ty),*> $crate::WithSafety<$crate::markers::Safe> for $fn_type {
107+
impl<Ret, $($ty),*> $crate::WithSafety<$crate::marker::Safe> for $fn_type {
108108
type F = extern $call_conv fn($($ty),*) -> Ret;
109109
}
110110
#[automatically_derived]
111-
impl<Ret, $($ty),*> $crate::WithSafety<$crate::markers::Unsafe> for $fn_type {
111+
impl<Ret, $($ty),*> $crate::WithSafety<$crate::marker::Unsafe> for $fn_type {
112112
type F = unsafe extern $call_conv fn($($ty),*) -> Ret;
113113
}
114114

115115
// HasAbi
116116
#[automatically_derived]
117-
impl<Ret, $($ty),*> $crate::HasAbi<$crate::markers::$abi_ident> for $fn_type {}
117+
impl<Ret, $($ty),*> $crate::HasAbi<$crate::marker::$abi_ident> for $fn_type {}
118118

119119
// WithAbi
120120
impl_fn!(@impl_withabi ($($nm : $ty),*), $fn_type, $safety, "Rust");
@@ -166,24 +166,24 @@ macro_rules! impl_fn {
166166

167167
(@impl_withabi ($($nm:ident : $ty:ident),*), $fn_type:ty, $safety:tt, $abi:tt) => {
168168
#[automatically_derived]
169-
impl<Ret, $($ty),*> $crate::WithAbi<$crate::markers::abi!($abi)> for $fn_type {
169+
impl<Ret, $($ty),*> $crate::WithAbi<$crate::abi!($abi)> for $fn_type {
170170
type F = impl_fn!(@make_unsafe extern $abi fn($($ty),*) -> Ret, $safety);
171171
}
172172
};
173173

174-
(@arity_marker ()) => { $crate::markers::A0 };
175-
(@arity_marker ($a:ty)) => { $crate::markers::A1 };
176-
(@arity_marker ($a:ty, $b:ty)) => { $crate::markers::A2 };
177-
(@arity_marker ($a:ty, $b:ty, $c:ty)) => { $crate::markers::A3 };
178-
(@arity_marker ($a:ty, $b:ty, $c:ty, $d:ty)) => { $crate::markers::A4 };
179-
(@arity_marker ($a:ty, $b:ty, $c:ty, $d:ty, $e:ty)) => { $crate::markers::A5 };
180-
(@arity_marker ($a:ty, $b:ty, $c:ty, $d:ty, $e:ty, $f:ty)) => { $crate::markers::A6 };
181-
(@arity_marker ($a:ty, $b:ty, $c:ty, $d:ty, $e:ty, $f:ty, $g:ty)) => { $crate::markers::A7 };
182-
(@arity_marker ($a:ty, $b:ty, $c:ty, $d:ty, $e:ty, $f:ty, $g:ty, $h:ty)) => { $crate::markers::A8 };
183-
(@arity_marker ($a:ty, $b:ty, $c:ty, $d:ty, $e:ty, $f:ty, $g:ty, $h:ty, $i:ty)) => { $crate::markers::A9 };
184-
(@arity_marker ($a:ty, $b:ty, $c:ty, $d:ty, $e:ty, $f:ty, $g:ty, $h:ty, $i:ty, $j:ty)) => { $crate::markers::A10 };
185-
(@arity_marker ($a:ty, $b:ty, $c:ty, $d:ty, $e:ty, $f:ty, $g:ty, $h:ty, $i:ty, $j:ty, $k:ty)) => { $crate::markers::A11 };
186-
(@arity_marker ($a:ty, $b:ty, $c:ty, $d:ty, $e:ty, $f:ty, $g:ty, $h:ty, $i:ty, $j:ty, $k:ty, $l:ty)) => { $crate::markers::A12 };
174+
(@arity_marker ()) => { $crate::marker::A0 };
175+
(@arity_marker ($a:ty)) => { $crate::marker::A1 };
176+
(@arity_marker ($a:ty, $b:ty)) => { $crate::marker::A2 };
177+
(@arity_marker ($a:ty, $b:ty, $c:ty)) => { $crate::marker::A3 };
178+
(@arity_marker ($a:ty, $b:ty, $c:ty, $d:ty)) => { $crate::marker::A4 };
179+
(@arity_marker ($a:ty, $b:ty, $c:ty, $d:ty, $e:ty)) => { $crate::marker::A5 };
180+
(@arity_marker ($a:ty, $b:ty, $c:ty, $d:ty, $e:ty, $f:ty)) => { $crate::marker::A6 };
181+
(@arity_marker ($a:ty, $b:ty, $c:ty, $d:ty, $e:ty, $f:ty, $g:ty)) => { $crate::marker::A7 };
182+
(@arity_marker ($a:ty, $b:ty, $c:ty, $d:ty, $e:ty, $f:ty, $g:ty, $h:ty)) => { $crate::marker::A8 };
183+
(@arity_marker ($a:ty, $b:ty, $c:ty, $d:ty, $e:ty, $f:ty, $g:ty, $h:ty, $i:ty)) => { $crate::marker::A9 };
184+
(@arity_marker ($a:ty, $b:ty, $c:ty, $d:ty, $e:ty, $f:ty, $g:ty, $h:ty, $i:ty, $j:ty)) => { $crate::marker::A10 };
185+
(@arity_marker ($a:ty, $b:ty, $c:ty, $d:ty, $e:ty, $f:ty, $g:ty, $h:ty, $i:ty, $j:ty, $k:ty)) => { $crate::marker::A11 };
186+
(@arity_marker ($a:ty, $b:ty, $c:ty, $d:ty, $e:ty, $f:ty, $g:ty, $h:ty, $i:ty, $j:ty, $k:ty, $l:ty)) => { $crate::marker::A12 };
187187

188188
(@make_unsafe $fn_type:ty, true) => { $fn_type };
189189
(@make_unsafe extern $abi:literal fn($($args:ty),*) -> $ret:ty, false) => {

src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@
8383
//! Or at the instance level:
8484
//!
8585
//! ```rust
86-
//! use fn_ptr::{FnPtr, markers};
86+
//! use fn_ptr::{FnPtr, abi};
8787
//! let rust_add: fn(i32, i32) -> i32 = |a, b| {a + b};
8888
//! // Safety: not actually safe!
89-
//! let c_add: extern "C" fn(i32, i32) -> i32 = unsafe { rust_add.with_abi::<markers::abi!("C")>() };
89+
//! let c_add: extern "C" fn(i32, i32) -> i32 = unsafe { rust_add.with_abi::<abi!("C")>() };
9090
//! # assert_eq!(rust_add.addr(), c_add.addr());
9191
//! ```
9292
//!
@@ -118,7 +118,7 @@ pub use abi::Abi;
118118
mod r#impl;
119119

120120
/// Module containing all marker types and traits.
121-
pub mod markers;
121+
pub mod marker;
122122

123123
/// Prelude for this crate.
124124
pub mod prelude;

0 commit comments

Comments
 (0)