Skip to content

Commit 1ddb6a0

Browse files
Removed tuple_tree and internal fn/method-related macros
1 parent d6fb70a commit 1ddb6a0

File tree

1 file changed

+16
-163
lines changed

1 file changed

+16
-163
lines changed

src/lib.rs

Lines changed: 16 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -142,194 +142,47 @@ extern crate alloc;
142142
/// ```
143143
#[doc = include_str!("../simple_examples/fn_add_three/src/main.rs")]
144144
/// ```
145-
/// This does NOT accept closures, since, as of Rust 1.91.0, closures cannot be `unsafe`.
145+
/// This does NOT accept closures, since, (as of Rust 1.91.0) closures cannot be `unsafe`.
146146
#[macro_export]
147147
macro_rules! unsafe_fn {
148-
( $fn:expr $(, $arg:expr)+ ) => {
149-
// Enclosed in a block, so that
150-
// 1. the result can be used as a value in an outer expression, and
151-
// 2. local variables don't conflict with the outer scope
148+
( $fn:expr $(, $arg:expr)* ) => {
149+
// Enclosed in a block, so that the result can be used as a value in an outer expression.
152150
{
153-
//@TODO remove accessors. Instead:
154-
// - if false {...}: assign the tuple. Then unreachable!();
155-
// - else {...}: original full call.
156-
let (tuple_tree, fun) = ($crate::unsafe_fn_internal_build_tuple_tree!{ $($arg),+ }, $fn);
157-
158-
$crate::unsafe_fn_internal_build_accessors_and_call! {
159-
fun,
160-
tuple_tree,
161-
( $( $arg ),* ),
162-
(0)
163-
}
164-
}
165-
};
166-
($fn:expr) => {
167-
{
168-
let fun = $fn;
169-
#[allow(unsafe_code)]
170-
unsafe {
171-
fun()
151+
if false {
152+
let _ = $fn;
153+
$( let _ = $arg; )*
154+
unreachable!()
155+
} else {
156+
#[allow(unsafe_code)]
157+
unsafe {
158+
$fn( $( $arg ),* )
159+
}
172160
}
173161
}
174162
};
175163
}
176164

177-
#[doc(hidden)]
178-
#[macro_export]
179-
macro_rules! unsafe_fn_internal_build_tuple_tree {
180-
// Construct the tuple_tree. Recursive:
181-
( $first:expr, $($rest:expr),+ ) => {
182-
(
183-
$first, $crate::unsafe_fn_internal_build_tuple_tree!{ $($rest),+ }
184-
)
185-
};
186-
( $last:expr) => {
187-
($last,)
188-
};
189-
}
190-
191-
#[doc(hidden)]
192-
#[macro_export]
193-
macro_rules! unsafe_fn_internal_build_accessors_and_call {
194-
// Access tuple_tree parts and get ready to call the function:
195-
( $fn:expr, $tuple_tree:ident,
196-
( $_first_arg:expr, $($other_arg:expr),+ ),
197-
$( ( $($accessor_part:tt),+
198-
)
199-
),*
200-
) => {
201-
$crate::unsafe_fn_internal_build_accessors_and_call!{
202-
$fn, $tuple_tree, ( $($other_arg),+ ),
203-
// Insert a new accessor to front (left): 0.
204-
(0),
205-
$( // Prepend 1 to each supplied/existing accessor
206-
( 1, $($accessor_part),+ )
207-
),*
208-
}
209-
};
210-
// All accessors are ready, so call the function:
211-
( $fn:expr, $tuple_tree:ident,
212-
( $_last_or_only_arg:expr ),
213-
$( ( $($accessor_part:tt),+
214-
)
215-
),*
216-
) => {
217-
#[allow(unsafe_code)]
218-
unsafe {
219-
$fn( $(
220-
$crate::unsafe_fn_internal_access_tuple_tree_field!{ $tuple_tree, $($accessor_part),+ }
221-
),*
222-
)
223-
}
224-
};
225-
}
226-
227-
#[doc(hidden)]
228-
#[macro_export]
229-
/// INTERNAL. Do NOT use directly - subject to change.
230-
///
231-
/// Expand an accessor group/list to access a field in the tuple_tree.
232-
macro_rules! unsafe_fn_internal_access_tuple_tree_field {
233-
( $tuple_tree:ident, $($accessor_part:tt),* ) => {
234-
$tuple_tree $(. $accessor_part )*
235-
};
236-
}
237-
//-------------
238-
239165
/// Invoke an `unsafe` method. Like [unsafe_fn], but
240166
/// - This accepts a receiver `&self`, `&mut self` and `self`. TODO Box/Rc/Arc, dyn?
241167
/// - This treats `self` as if in an `unsafe {...}` block.
242168
/// - $fn can **NOT** be an expression or a qualified path (which doesn't work in standard methods
243169
/// calls anyways), but only an identifier.
244170
#[macro_export]
245171
macro_rules! unsafe_method {
246-
($self:expr, $fn:ident $(, $arg:expr)+ ) => {
172+
($self:expr, $fn:ident $(, $arg:expr)* ) => {
247173
// Enclosed in a block, so that the result can be used as a value in an outer expression
248174
// without upsetting operator precedence.
249175
{
250176
if false {
251-
// Receiver has to be mutable, so that this also works for methods that take &mut
252-
// self.
253-
let (tuple_tree, mut receiver) = (
254-
$crate::unsafe_fn_internal_build_tuple_tree!{ $($arg),+ },
255-
$self
256-
);
257-
// Assign the result, in case the method is `#[must_use]`
258-
let _ = $crate::unsafe_method_internal! {
259-
receiver,
260-
$fn,
261-
tuple_tree,
262-
( $( $arg ),* ),
263-
(0)
264-
};
177+
let _ = $self;
178+
$( let _ = $arg; )*
265179
unreachable!()
266180
} else {
181+
#[allow(unsafe_code)]
267182
unsafe { $self. $fn ( $( $arg ),* ) }
268183
}
269184
}
270185
};
271-
272-
($self:expr, $fn:ident ) => {
273-
// Enclosed in a block, so that the result can be used as a value in an outer expression
274-
// without upsetting operator precedence.
275-
{
276-
if false {
277-
let mut receiver = $self;
278-
// Assign the result, in case the method is `#[must_use]`
279-
let _ = {
280-
#[allow(unsafe_code)]
281-
unsafe {
282-
receiver. $fn()
283-
}
284-
};
285-
unreachable!()
286-
} else {
287-
$self. $fn ()
288-
}
289-
}
290-
};
291-
}
292-
293-
#[doc(hidden)]
294-
#[macro_export]
295-
macro_rules! unsafe_method_internal {
296-
// Access tuple_tree parts and get ready to call the method:
297-
( $self:expr, $fn:ident, $tuple_tree:ident,
298-
( $_first_arg:expr, $($other_arg:expr),+ ),
299-
$( ( $($accessor_part:tt),+
300-
)
301-
),*
302-
) => {
303-
$crate::unsafe_method_internal!{
304-
$self, $fn, $tuple_tree, ( $($other_arg),+ ),
305-
// Insert a new accessor to front (left): 0.
306-
(0),
307-
$( // Prepend 1 to each supplied/existing accessor
308-
( 1, $($accessor_part),+ )
309-
),*
310-
}
311-
};
312-
// All accessors are ready. $self was already evaluated (outside of unsafe {...}). So call the
313-
// function:
314-
( $self:expr, $fn:ident, $tuple_tree:ident,
315-
( $_last_or_only_arg:expr ),
316-
$( ( $($accessor_part:tt),+
317-
)
318-
),*
319-
) => {
320-
// Extra block needed, in case the result is assigned to a variable. Otherwise surprise:
321-
// "attributes on expressions are experimental"
322-
// https://github.com/rust-lang/rust/issues/15701
323-
{
324-
#[allow(unsafe_code)]
325-
unsafe {
326-
$self. $fn( $(
327-
$crate::unsafe_fn_internal_access_tuple_tree_field!{ $tuple_tree, $($accessor_part),+ }
328-
),*
329-
)
330-
}
331-
}
332-
};
333186
}
334187
//-------------
335188

0 commit comments

Comments
 (0)