35
35
36
36
#include < new>
37
37
38
+ #if __cplusplus < 202002l || !defined(__cpp_lib_bit_cast)
39
+ namespace std {
40
+ template <typename Destination, typename Source>
41
+ std::enable_if_t <sizeof (Destination) == sizeof (Source) &&
42
+ std::is_trivially_copyable_v<Source> &&
43
+ std::is_trivially_copyable_v<Destination>, Destination>
44
+ bit_cast (const Source &src) noexcept {
45
+ static_assert (std::is_trivially_constructible_v<Destination>,
46
+ " The destination type must be trivially constructible" );
47
+ Destination dst;
48
+ if constexpr (std::is_pointer_v<Source> || std::is_pointer_v<Destination>)
49
+ std::memcpy (reinterpret_cast <uintptr_t *>(&dst),
50
+ reinterpret_cast <const uintptr_t *>(&src), sizeof (Destination));
51
+ else
52
+ std::memcpy (&dst, &src, sizeof (Destination));
53
+ return dst;
54
+ }
55
+ }
56
+ #else
57
+ #include < bit>
58
+ #endif
59
+
38
60
using namespace swift ;
39
61
40
62
namespace {
@@ -287,8 +309,8 @@ static void _asyncLet_get_throwing_continuation(
287
309
}
288
310
289
311
// Continue the caller's execution.
290
- auto throwingResume
291
- = reinterpret_cast <ThrowingTaskFutureWaitContinuationFunction*>(callContext->ResumeParent );
312
+ auto throwingResume =
313
+ std::bit_cast <ThrowingTaskFutureWaitContinuationFunction*>(callContext->ResumeParent );
292
314
return throwingResume (callContext->Parent , error);
293
315
}
294
316
@@ -305,8 +327,8 @@ static void swift_asyncLet_get_throwingImpl(
305
327
}
306
328
307
329
auto aletContext = static_cast <AsyncLetContinuationContext*>(callContext);
308
- aletContext->ResumeParent
309
- = reinterpret_cast <TaskContinuationFunction*>(resumeFunction);
330
+ aletContext->ResumeParent =
331
+ std::bit_cast <TaskContinuationFunction*>(resumeFunction);
310
332
aletContext->Parent = callerContext;
311
333
aletContext->alet = alet;
312
334
auto futureContext = asImpl (alet)->getFutureContext ();
@@ -376,7 +398,7 @@ static void asyncLet_finish_after_task_completion(SWIFT_ASYNC_CONTEXT AsyncConte
376
398
swift_task_dealloc (task);
377
399
}
378
400
379
- return reinterpret_cast <ThrowingTaskFutureWaitContinuationFunction*>(resumeFunction)
401
+ return std::bit_cast <ThrowingTaskFutureWaitContinuationFunction*>(resumeFunction)
380
402
(callerContext, error);
381
403
}
382
404
@@ -528,14 +550,14 @@ static void swift_asyncLet_consume_throwingImpl(
528
550
if (asImpl (alet)->hasResultInBuffer ()) {
529
551
return asyncLet_finish_after_task_completion (callerContext,
530
552
alet,
531
- reinterpret_cast <TaskContinuationFunction*>(resumeFunction),
553
+ std::bit_cast <TaskContinuationFunction*>(resumeFunction),
532
554
callContext,
533
555
nullptr );
534
556
}
535
557
536
558
auto aletContext = static_cast <AsyncLetContinuationContext*>(callContext);
537
- aletContext->ResumeParent
538
- = reinterpret_cast <TaskContinuationFunction*>(resumeFunction);
559
+ aletContext->ResumeParent =
560
+ std::bit_cast <TaskContinuationFunction*>(resumeFunction);
539
561
aletContext->Parent = callerContext;
540
562
aletContext->alet = alet;
541
563
auto futureContext = asImpl (alet)->getFutureContext ();
0 commit comments