gsp_wasm_interface_common/util.rs
1// This file is part of Gear.
2//
3// Copyright (C) 2021-2023 Gear Technologies Inc.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6use core::ops::Range;
7
8/// Construct a range from an offset to a data length after the offset.
9/// Returns None if the end of the range would exceed some maximum offset.
10pub fn checked_range(offset: usize, len: usize, max: usize) -> Option<Range<usize>> {
11 let end = offset.checked_add(len)?;
12 (end <= max).then(|| offset..end)
13}