From f717377a4ac8134224e4162933aaeb0a32380486 Mon Sep 17 00:00:00 2001 From: playX18 <158266309+playX18@users.noreply.github.com> Date: Wed, 2 Oct 2024 15:47:37 +0700 Subject: [PATCH] refactor(gstd): use waker-fn instead of unsafe code (#4271) --- Cargo.lock | 5 +++-- gstd/Cargo.toml | 1 + gstd/src/async_runtime/futures.rs | 2 +- gstd/src/async_runtime/mod.rs | 1 - gstd/src/async_runtime/waker.rs | 37 ------------------------------- 5 files changed, 5 insertions(+), 41 deletions(-) delete mode 100644 gstd/src/async_runtime/waker.rs diff --git a/Cargo.lock b/Cargo.lock index ca1df6deefd..bc19d620dbe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7472,6 +7472,7 @@ dependencies = [ "hex", "parity-scale-codec", "scale-info", + "waker-fn", ] [[package]] @@ -19027,9 +19028,9 @@ dependencies = [ [[package]] name = "waker-fn" -version = "1.1.0" +version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d5b2c62b4012a3e1eca5a7e077d13b3bf498c4073e33ccd58626607748ceeca" +checksum = "317211a0dc0ceedd78fb2ca9a44aed3d7b9b26f81870d485c07122b4350673b7" [[package]] name = "walkdir" diff --git a/gstd/Cargo.toml b/gstd/Cargo.toml index 46e61e53c68..b9c6645045b 100644 --- a/gstd/Cargo.toml +++ b/gstd/Cargo.toml @@ -25,6 +25,7 @@ hex = { workspace = true, features = ["alloc"] } parity-scale-codec = { workspace = true, features = ["derive"] } scale-info = { workspace = true, features = ["derive"] } futures = { workspace = true, features = ["alloc"] } +waker-fn = "1.2.0" [features] #! ## Default features diff --git a/gstd/src/async_runtime/futures.rs b/gstd/src/async_runtime/futures.rs index 43fb8a1b05f..2e4c88e4d99 100644 --- a/gstd/src/async_runtime/futures.rs +++ b/gstd/src/async_runtime/futures.rs @@ -47,7 +47,7 @@ impl Task { F: Future + 'static, { Self { - waker: super::waker::empty(), + waker: waker_fn::waker_fn(|| {}), future: future.boxed_local(), lock_exceeded: false, } diff --git a/gstd/src/async_runtime/mod.rs b/gstd/src/async_runtime/mod.rs index 9a0c07ee76c..ceb8bd3eb9c 100644 --- a/gstd/src/async_runtime/mod.rs +++ b/gstd/src/async_runtime/mod.rs @@ -19,7 +19,6 @@ mod futures; mod locks; mod signals; -mod waker; #[cfg(not(feature = "ethexe"))] mod reply_hooks; diff --git a/gstd/src/async_runtime/waker.rs b/gstd/src/async_runtime/waker.rs deleted file mode 100644 index e16f56c908b..00000000000 --- a/gstd/src/async_runtime/waker.rs +++ /dev/null @@ -1,37 +0,0 @@ -// This file is part of Gear. - -// Copyright (C) 2021-2024 Gear Technologies Inc. -// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 - -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. - -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -//! Module for Gear programs asynchronous waker. - -use core::{ - ptr, - task::{RawWaker, RawWakerVTable, Waker}, -}; - -const VTABLE: RawWakerVTable = RawWakerVTable::new(clone_waker, wake, wake_by_ref, drop_waker); - -pub(crate) fn empty() -> Waker { - unsafe { Waker::from_raw(RawWaker::new(ptr::null(), &VTABLE)) } -} - -unsafe fn clone_waker(ptr: *const ()) -> RawWaker { - RawWaker::new(ptr, &VTABLE) -} -unsafe fn wake(_ptr: *const ()) {} -unsafe fn wake_by_ref(_ptr: *const ()) {} -unsafe fn drop_waker(_ptr: *const ()) {}