From dfcaaa5843270dced9a8fc1050bd1b65ef8a1e1e Mon Sep 17 00:00:00 2001 From: clearloop <26088946+clearloop@users.noreply.github.com> Date: Fri, 22 Dec 2023 08:11:58 +0800 Subject: [PATCH] chore(wasm-interface): re-export checked_range from sp-wasm-interface-common --- primitives/wasm-interface-common/src/lib.rs | 2 ++ primitives/wasm-interface-common/src/util.rs | 24 ++++++++++++++++++++ primitives/wasm-interface/src/util.rs | 8 +------ 3 files changed, 27 insertions(+), 7 deletions(-) create mode 100644 primitives/wasm-interface-common/src/util.rs diff --git a/primitives/wasm-interface-common/src/lib.rs b/primitives/wasm-interface-common/src/lib.rs index 6d021a1e3553a..18d5a97592039 100644 --- a/primitives/wasm-interface-common/src/lib.rs +++ b/primitives/wasm-interface-common/src/lib.rs @@ -22,6 +22,8 @@ use sp_std::borrow::Cow; use core::{mem, marker::PhantomData}; +pub mod util; + #[cfg(feature = "wasmi")] pub use wasmi; diff --git a/primitives/wasm-interface-common/src/util.rs b/primitives/wasm-interface-common/src/util.rs new file mode 100644 index 0000000000000..5f3a259434304 --- /dev/null +++ b/primitives/wasm-interface-common/src/util.rs @@ -0,0 +1,24 @@ +// This file is part of Gear. +// +// Copyright (C) 2021-2023 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 . + +/// Construct a range from an offset to a data length after the offset. +/// Returns None if the end of the range would exceed some maximum offset. +pub fn checked_range(offset: usize, len: usize, max: usize) -> Option> { + let end = offset.checked_add(len)?; + (end <= max).then(|| offset..end) +} diff --git a/primitives/wasm-interface/src/util.rs b/primitives/wasm-interface/src/util.rs index 52a59cc2ae8c1..0e37d889c21a2 100644 --- a/primitives/wasm-interface/src/util.rs +++ b/primitives/wasm-interface/src/util.rs @@ -18,13 +18,7 @@ use super::*; use wasmtime::{AsContext, AsContextMut}; use sp_std::ops::Range; - -/// Construct a range from an offset to a data length after the offset. -/// Returns None if the end of the range would exceed some maximum offset. -pub fn checked_range(offset: usize, len: usize, max: usize) -> Option> { - let end = offset.checked_add(len)?; - (end <= max).then(|| offset..end) -} +pub use sp_wasm_interface_common::util::checked_range; pub fn write_memory_from( mut ctx: impl AsContextMut,