|
448 | 448 | <a href="#448" id="448">448</a>
|
449 | 449 | <a href="#449" id="449">449</a>
|
450 | 450 | <a href="#450" id="450">450</a>
|
451 |
| -<a href="#451" id="451">451</a> |
452 |
| -<a href="#452" id="452">452</a> |
453 |
| -<a href="#453" id="453">453</a> |
454 |
| -<a href="#454" id="454">454</a> |
455 | 451 | </pre></div><pre class="rust"><code><span class="kw">use crate</span>::{
|
456 | 452 | binding_model,
|
457 | 453 | hal_api::HalApi,
|
|
727 | 723 | }
|
728 | 724 | }
|
729 | 725 |
|
730 |
| - <span class="attr">#[allow(trivial_casts)] |
731 |
| - </span><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) { |
| 726 | + <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) { |
732 | 727 | <span class="kw">match </span><span class="self">self</span>.inner {
|
733 | 728 | DeviceLostClosureInner::Rust { callback } => callback(reason, message),
|
734 | 729 | <span class="comment">// SAFETY: the contract of the call to from_c says that this unsafe is sound.
|
735 | 730 | </span>DeviceLostClosureInner::C { inner } => <span class="kw">unsafe </span>{
|
736 |
| - <span class="comment">// We need to pass message as a c_char typed pointer. To avoid trivial |
737 |
| - // conversion warnings on some platforms, we use the allow lint. |
738 |
| - </span>(inner.callback)( |
739 |
| - inner.user_data, |
740 |
| - reason <span class="kw">as </span>u8, |
741 |
| - message.as_ptr() <span class="kw">as </span><span class="kw-2">*const </span>c_char, |
742 |
| - ) |
| 731 | + <span class="comment">// Ensure message is structured as a null-terminated C string. It only |
| 732 | + // needs to live as long as the callback invocation. |
| 733 | + </span><span class="kw">let </span>message = std::ffi::CString::new(message).unwrap(); |
| 734 | + (inner.callback)(inner.user_data, reason <span class="kw">as </span>u8, message.as_ptr()) |
743 | 735 | },
|
744 | 736 | }
|
745 | 737 | }
|
|
0 commit comments