Skip to content

Commit

Permalink
chore(wasm-interface): re-export checked_range from sp-wasm-interface…
Browse files Browse the repository at this point in the history
…-common
  • Loading branch information
clearloop committed Dec 22, 2023
1 parent 27779e0 commit dfcaaa5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
2 changes: 2 additions & 0 deletions primitives/wasm-interface-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
use sp_std::borrow::Cow;
use core::{mem, marker::PhantomData};

pub mod util;

#[cfg(feature = "wasmi")]
pub use wasmi;

Expand Down
24 changes: 24 additions & 0 deletions primitives/wasm-interface-common/src/util.rs
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.

/// 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<Range<usize>> {
let end = offset.checked_add(len)?;
(end <= max).then(|| offset..end)
}
8 changes: 1 addition & 7 deletions primitives/wasm-interface/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Range<usize>> {
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<Data = StoreData>,
Expand Down

0 comments on commit dfcaaa5

Please sign in to comment.