|
458 | 458 | <a href="#457" id="457">457</a> |
459 | 459 | <a href="#458" id="458">458</a> |
460 | 460 | <a href="#459" id="459">459</a> |
461 | | -<a href="#460" id="460">460</a> |
462 | | -<a href="#461" id="461">461</a> |
463 | | -<a href="#462" id="462">462</a> |
464 | | -<a href="#463" id="463">463</a> |
465 | | -<a href="#464" id="464">464</a> |
466 | | -<a href="#465" id="465">465</a> |
467 | | -<a href="#466" id="466">466</a> |
468 | | -<a href="#467" id="467">467</a> |
469 | | -<a href="#468" id="468">468</a> |
470 | | -<a href="#469" id="469">469</a> |
471 | | -<a href="#470" id="470">470</a> |
472 | | -<a href="#471" id="471">471</a> |
473 | | -<a href="#472" id="472">472</a> |
474 | | -<a href="#473" id="473">473</a> |
475 | | -<a href="#474" id="474">474</a> |
476 | | -<a href="#475" id="475">475</a> |
477 | | -<a href="#476" id="476">476</a> |
478 | | -<a href="#477" id="477">477</a> |
479 | | -<a href="#478" id="478">478</a> |
480 | | -<a href="#479" id="479">479</a> |
481 | | -<a href="#480" id="480">480</a> |
482 | | -<a href="#481" id="481">481</a> |
483 | | -<a href="#482" id="482">482</a> |
484 | | -<a href="#483" id="483">483</a> |
485 | | -<a href="#484" id="484">484</a> |
486 | | -<a href="#485" id="485">485</a> |
487 | | -<a href="#486" id="486">486</a> |
488 | | -<a href="#487" id="487">487</a> |
489 | | -<a href="#488" id="488">488</a> |
490 | | -<a href="#489" id="489">489</a> |
491 | | -<a href="#490" id="490">490</a> |
492 | | -<a href="#491" id="491">491</a> |
493 | | -<a href="#492" id="492">492</a> |
494 | | -<a href="#493" id="493">493</a> |
495 | | -<a href="#494" id="494">494</a> |
496 | | -<a href="#495" id="495">495</a> |
497 | | -<a href="#496" id="496">496</a> |
498 | | -<a href="#497" id="497">497</a> |
499 | | -<a href="#498" id="498">498</a> |
500 | | -<a href="#499" id="499">499</a> |
501 | | -<a href="#500" id="500">500</a> |
502 | | -<a href="#501" id="501">501</a> |
503 | | -<a href="#502" id="502">502</a> |
504 | | -<a href="#503" id="503">503</a> |
505 | | -<a href="#504" id="504">504</a> |
506 | | -<a href="#505" id="505">505</a> |
507 | | -<a href="#506" id="506">506</a> |
508 | | -<a href="#507" id="507">507</a> |
509 | | -<a href="#508" id="508">508</a> |
510 | | -<a href="#509" id="509">509</a> |
511 | | -<a href="#510" id="510">510</a> |
512 | | -<a href="#511" id="511">511</a> |
513 | | -<a href="#512" id="512">512</a> |
514 | | -<a href="#513" id="513">513</a> |
515 | | -<a href="#514" id="514">514</a> |
516 | | -<a href="#515" id="515">515</a> |
517 | | -<a href="#516" id="516">516</a> |
518 | | -<a href="#517" id="517">517</a> |
519 | | -<a href="#518" id="518">518</a> |
520 | | -<a href="#519" id="519">519</a> |
521 | | -<a href="#520" id="520">520</a> |
522 | | -<a href="#521" id="521">521</a> |
523 | | -<a href="#522" id="522">522</a> |
524 | | -<a href="#523" id="523">523</a> |
525 | | -<a href="#524" id="524">524</a> |
526 | | -<a href="#525" id="525">525</a> |
527 | | -<a href="#526" id="526">526</a> |
528 | | -<a href="#527" id="527">527</a> |
529 | | -<a href="#528" id="528">528</a> |
530 | | -<a href="#529" id="529">529</a> |
531 | | -<a href="#530" id="530">530</a> |
532 | | -<a href="#531" id="531">531</a> |
533 | | -<a href="#532" id="532">532</a> |
534 | | -<a href="#533" id="533">533</a> |
535 | | -<a href="#534" id="534">534</a> |
536 | | -<a href="#535" id="535">535</a> |
537 | | -<a href="#536" id="536">536</a> |
538 | | -<a href="#537" id="537">537</a> |
539 | | -<a href="#538" id="538">538</a> |
540 | | -<a href="#539" id="539">539</a> |
541 | | -<a href="#540" id="540">540</a> |
542 | | -<a href="#541" id="541">541</a> |
543 | | -<a href="#542" id="542">542</a> |
544 | | -<a href="#543" id="543">543</a> |
545 | | -<a href="#544" id="544">544</a> |
546 | | -<a href="#545" id="545">545</a> |
547 | | -<a href="#546" id="546">546</a> |
548 | | -<a href="#547" id="547">547</a> |
549 | | -<a href="#548" id="548">548</a> |
550 | | -<a href="#549" id="549">549</a> |
551 | | -<a href="#550" id="550">550</a> |
552 | | -<a href="#551" id="551">551</a> |
553 | | -<a href="#552" id="552">552</a> |
554 | | -<a href="#553" id="553">553</a> |
555 | | -<a href="#554" id="554">554</a> |
556 | | -<a href="#555" id="555">555</a> |
557 | | -<a href="#556" id="556">556</a> |
558 | | -<a href="#557" id="557">557</a> |
559 | | -<a href="#558" id="558">558</a> |
560 | | -<a href="#559" id="559">559</a> |
561 | 461 | </pre></div><pre class="rust"><code><span class="kw">use crate</span>::{ |
562 | 462 | binding_model, |
563 | 463 | hub::Hub, |
|
572 | 472 |
|
573 | 473 | <span class="kw">use </span>arrayvec::ArrayVec; |
574 | 474 | <span class="kw">use </span>smallvec::SmallVec; |
575 | | -<span class="kw">use </span>std::os::raw::c_char; |
576 | 475 | <span class="kw">use </span>thiserror::Error; |
577 | 476 | <span class="kw">use </span>wgt::{BufferAddress, DeviceLostReason, TextureFormat}; |
578 | 477 |
|
|
736 | 635 | // a on_submitted_work_done callback to be fired before the on_submitted_work_done callback. |
737 | 636 | </span><span class="kw">for </span>(<span class="kw-2">mut </span>operation, status) <span class="kw">in </span><span class="self">self</span>.mappings { |
738 | 637 | <span class="kw">if let </span><span class="prelude-val">Some</span>(callback) = operation.callback.take() { |
739 | | - callback.call(status); |
| 638 | + callback(status); |
740 | 639 | } |
741 | 640 | } |
742 | 641 | <span class="kw">for </span>closure <span class="kw">in </span><span class="self">self</span>.submissions { |
743 | | - closure.call(); |
| 642 | + closure(); |
744 | 643 | } |
745 | 644 | <span class="kw">for </span>invocation <span class="kw">in </span><span class="self">self</span>.device_lost_invocations { |
746 | | - invocation |
747 | | - .closure |
748 | | - .call(invocation.reason, invocation.message); |
| 645 | + (invocation.closure)(invocation.reason, invocation.message); |
749 | 646 | } |
750 | 647 | } |
751 | 648 | } |
752 | 649 |
|
753 | 650 | <span class="attr">#[cfg(send_sync)] |
754 | | -</span><span class="kw">pub type </span>DeviceLostCallback = Box<<span class="kw">dyn </span>Fn(DeviceLostReason, String) + Send + <span class="lifetime">'static</span>>; |
| 651 | +</span><span class="kw">pub type </span>DeviceLostClosure = Box<<span class="kw">dyn </span>FnOnce(DeviceLostReason, String) + Send + <span class="lifetime">'static</span>>; |
755 | 652 | <span class="attr">#[cfg(not(send_sync))] |
756 | | -</span><span class="kw">pub type </span>DeviceLostCallback = Box<<span class="kw">dyn </span>Fn(DeviceLostReason, String) + <span class="lifetime">'static</span>>; |
757 | | - |
758 | | -<span class="kw">pub struct </span>DeviceLostClosureRust { |
759 | | - <span class="kw">pub </span>callback: DeviceLostCallback, |
760 | | - consumed: bool, |
761 | | -} |
762 | | - |
763 | | -<span class="kw">impl </span>Drop <span class="kw">for </span>DeviceLostClosureRust { |
764 | | - <span class="kw">fn </span>drop(<span class="kw-2">&mut </span><span class="self">self</span>) { |
765 | | - <span class="kw">if </span>!<span class="self">self</span>.consumed { |
766 | | - <span class="macro">panic!</span>(<span class="string">"DeviceLostClosureRust must be consumed before it is dropped."</span>); |
767 | | - } |
768 | | - } |
769 | | -} |
770 | | - |
771 | | -<span class="attr">#[repr(C)] |
772 | | -</span><span class="kw">pub struct </span>DeviceLostClosureC { |
773 | | - <span class="kw">pub </span>callback: <span class="kw">unsafe extern </span><span class="string">"C" </span><span class="kw">fn</span>(user_data: <span class="kw-2">*mut </span>u8, reason: u8, message: <span class="kw-2">*const </span>c_char), |
774 | | - <span class="kw">pub </span>user_data: <span class="kw-2">*mut </span>u8, |
775 | | - consumed: bool, |
776 | | -} |
777 | | - |
778 | | -<span class="attr">#[cfg(send_sync)] |
779 | | -</span><span class="kw">unsafe impl </span>Send <span class="kw">for </span>DeviceLostClosureC {} |
780 | | - |
781 | | -<span class="kw">impl </span>Drop <span class="kw">for </span>DeviceLostClosureC { |
782 | | - <span class="kw">fn </span>drop(<span class="kw-2">&mut </span><span class="self">self</span>) { |
783 | | - <span class="kw">if </span>!<span class="self">self</span>.consumed { |
784 | | - <span class="macro">panic!</span>(<span class="string">"DeviceLostClosureC must be consumed before it is dropped."</span>); |
785 | | - } |
786 | | - } |
787 | | -} |
788 | | - |
789 | | -<span class="kw">pub struct </span>DeviceLostClosure { |
790 | | - <span class="comment">// We wrap this so creating the enum in the C variant can be unsafe, |
791 | | - // allowing our call function to be safe. |
792 | | - </span>inner: DeviceLostClosureInner, |
793 | | -} |
| 653 | +</span><span class="kw">pub type </span>DeviceLostClosure = Box<<span class="kw">dyn </span>FnOnce(DeviceLostReason, String) + <span class="lifetime">'static</span>>; |
794 | 654 |
|
795 | 655 | <span class="kw">pub struct </span>DeviceLostInvocation { |
796 | 656 | closure: DeviceLostClosure, |
797 | 657 | reason: DeviceLostReason, |
798 | 658 | message: String, |
799 | 659 | } |
800 | 660 |
|
801 | | -<span class="kw">enum </span>DeviceLostClosureInner { |
802 | | - Rust { inner: DeviceLostClosureRust }, |
803 | | - C { inner: DeviceLostClosureC }, |
804 | | -} |
805 | | - |
806 | | -<span class="kw">impl </span>DeviceLostClosure { |
807 | | - <span class="kw">pub fn </span>from_rust(callback: DeviceLostCallback) -> <span class="self">Self </span>{ |
808 | | - <span class="kw">let </span>inner = DeviceLostClosureRust { |
809 | | - callback, |
810 | | - consumed: <span class="bool-val">false</span>, |
811 | | - }; |
812 | | - <span class="self">Self </span>{ |
813 | | - inner: DeviceLostClosureInner::Rust { inner }, |
814 | | - } |
815 | | - } |
816 | | - |
817 | | - <span class="doccomment">/// # Safety |
818 | | - /// |
819 | | - /// - The callback pointer must be valid to call with the provided `user_data` |
820 | | - /// pointer. |
821 | | - /// |
822 | | - /// - Both pointers must point to `'static` data, as the callback may happen at |
823 | | - /// an unspecified time. |
824 | | - </span><span class="kw">pub unsafe fn </span>from_c(<span class="kw-2">mut </span>closure: DeviceLostClosureC) -> <span class="self">Self </span>{ |
825 | | - <span class="comment">// Build an inner with the values from closure, ensuring that |
826 | | - // inner.consumed is false. |
827 | | - </span><span class="kw">let </span>inner = DeviceLostClosureC { |
828 | | - callback: closure.callback, |
829 | | - user_data: closure.user_data, |
830 | | - consumed: <span class="bool-val">false</span>, |
831 | | - }; |
832 | | - |
833 | | - <span class="comment">// Mark the original closure as consumed, so we can safely drop it. |
834 | | - </span>closure.consumed = <span class="bool-val">true</span>; |
835 | | - |
836 | | - <span class="self">Self </span>{ |
837 | | - inner: DeviceLostClosureInner::C { inner }, |
838 | | - } |
839 | | - } |
840 | | - |
841 | | - <span class="kw">pub</span>(<span class="kw">crate</span>) <span class="kw">fn </span>call(<span class="self">self</span>, reason: DeviceLostReason, message: String) { |
842 | | - <span class="kw">match </span><span class="self">self</span>.inner { |
843 | | - DeviceLostClosureInner::Rust { <span class="kw-2">mut </span>inner } => { |
844 | | - inner.consumed = <span class="bool-val">true</span>; |
845 | | - |
846 | | - (inner.callback)(reason, message) |
847 | | - } |
848 | | - <span class="comment">// SAFETY: the contract of the call to from_c says that this unsafe is sound. |
849 | | - </span>DeviceLostClosureInner::C { <span class="kw-2">mut </span>inner } => <span class="kw">unsafe </span>{ |
850 | | - inner.consumed = <span class="bool-val">true</span>; |
851 | | - |
852 | | - <span class="comment">// Ensure message is structured as a null-terminated C string. It only |
853 | | - // needs to live as long as the callback invocation. |
854 | | - </span><span class="kw">let </span>message = std::ffi::CString::new(message).unwrap(); |
855 | | - (inner.callback)(inner.user_data, reason <span class="kw">as </span>u8, message.as_ptr()) |
856 | | - }, |
857 | | - } |
858 | | - } |
859 | | -} |
860 | | - |
861 | 661 | <span class="kw">pub</span>(<span class="kw">crate</span>) <span class="kw">fn </span>map_buffer( |
862 | 662 | buffer: <span class="kw-2">&</span>Buffer, |
863 | 663 | offset: BufferAddress, |
|
0 commit comments