// Copyright 2019-2020 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot 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.
// Polkadot 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 Polkadot. If not, see .
//! The SlotRange struct which succinctly handles the ten values that
//! represent all sub ranges between 0 and 3 inclusive.
use sp_std::{result, ops::Add, convert::{TryFrom, TryInto}};
use sp_runtime::traits::CheckedSub;
use parity_scale_codec::{Encode, Decode};
/// Total number of possible sub ranges of slots.
pub const SLOT_RANGE_COUNT: usize = 10;
/// A compactly represented sub-range from the series (0, 1, 2, 3).
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode)]
#[repr(u8)]
pub enum SlotRange {
/// Sub range from index 0 to index 0 inclusive.
ZeroZero = 0,
/// Sub range from index 0 to index 1 inclusive.
ZeroOne = 1,
/// Sub range from index 0 to index 2 inclusive.
ZeroTwo = 2,
/// Sub range from index 0 to index 3 inclusive.
ZeroThree = 3,
/// Sub range from index 1 to index 1 inclusive.
OneOne = 4,
/// Sub range from index 1 to index 2 inclusive.
OneTwo = 5,
/// Sub range from index 1 to index 3 inclusive.
OneThree = 6,
/// Sub range from index 2 to index 2 inclusive.
TwoTwo = 7,
/// Sub range from index 2 to index 3 inclusive.
TwoThree = 8,
/// Sub range from index 3 to index 3 inclusive.
ThreeThree = 9, // == SLOT_RANGE_COUNT - 1
}
#[cfg(feature = "std")]
impl std::fmt::Debug for SlotRange {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
let p = self.as_pair();
write!(fmt, "[{}..{}]", p.0, p.1)
}
}
impl SlotRange {
pub fn new_bounded<
Index: Add