Skip to content

Commit

Permalink
chore(wasm-interface): re-export checked_range from -common (#41)
Browse files Browse the repository at this point in the history
* chore(wasm-interface): re-export checked_range from sp-wasm-interface-common

* chore(wasm-interface-common): import Range from core
  • Loading branch information
clearloop authored and ukint-vs committed Jan 1, 2024
1 parent 092eb70 commit e4b3ebf
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 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
26 changes: 26 additions & 0 deletions primitives/wasm-interface-common/src/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 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/>.

use core::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)
}
9 changes: 1 addition & 8 deletions primitives/wasm-interface/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,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 e4b3ebf

Please sign in to comment.