Skip to content

Commit

Permalink
refactor: Removed one lifetime with the assumption all namespaces wil…
Browse files Browse the repository at this point in the history
…l be `&'static str`
  • Loading branch information
KirilMihaylov authored and Gancho Manev committed May 16, 2023
1 parent 7446f09 commit 30e8452
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 35 deletions.
4 changes: 2 additions & 2 deletions contracts/timealarms/src/alarms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ const REPLY_ID: Id = 0;
const EVENT_TYPE: &str = "timealarm";

pub(super) struct TimeAlarms<'r> {
time_alarms: Alarms<'r, 'static>,
time_alarms: Alarms<'r>,
}

pub(super) struct TimeAlarmsMut<'r> {
time_alarms: AlarmsMut<'r, 'static>,
time_alarms: AlarmsMut<'r>,
}

impl<'r> TimeAlarms<'r> {
Expand Down
39 changes: 18 additions & 21 deletions packages/time-oracle/src/alarms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,27 @@ fn as_seconds(from: Timestamp) -> TimeSeconds {
from.seconds()
}

struct AlarmIndexes<'a> {
alarms: MultiIndex<'a, TimeSeconds, TimeSeconds, Addr>,
struct AlarmIndexes {
alarms: MultiIndex<'static, TimeSeconds, TimeSeconds, Addr>,
}

impl<'a> IndexList<TimeSeconds> for AlarmIndexes<'a> {
impl IndexList<TimeSeconds> for AlarmIndexes {
fn get_indexes(&self) -> Box<dyn Iterator<Item = &'_ dyn Index<TimeSeconds>> + '_> {
let v: Vec<&dyn Index<TimeSeconds>> = vec![&self.alarms];

Box::new(v.into_iter())
}
}

fn indexed_map<'namespace>(
namespace_alarms: &'namespace str,
namespace_index: &'namespace str,
) -> IndexedMap<'namespace> {
fn indexed_map(namespace_alarms: &'static str, namespace_index: &'static str) -> IndexedMap {
let indexes = AlarmIndexes {
alarms: MultiIndex::new(|_, d| *d, namespace_alarms, namespace_index),
};

IndexedMap::new(namespace_alarms, indexes)
}

type IndexedMap<'namespace> = CwIndexedMap<'namespace, Addr, TimeSeconds, AlarmIndexes<'namespace>>;
type IndexedMap = CwIndexedMap<'static, Addr, TimeSeconds, AlarmIndexes>;

const ALARMS_IN_DELIVERY: Deque<'static, Addr> = Deque::new("in_delivery");

Expand All @@ -49,16 +46,16 @@ pub trait AlarmsSelection {
fn alarms_selection(&self, ctime: Timestamp) -> AlarmsSelectionIterator<'_>;
}

pub struct Alarms<'storage, 'namespace> {
pub struct Alarms<'storage> {
storage: &'storage dyn Storage,
alarms: IndexedMap<'namespace>,
alarms: IndexedMap,
}

impl<'storage, 'namespace> Alarms<'storage, 'namespace> {
impl<'storage> Alarms<'storage> {
pub fn new(
storage: &'storage dyn Storage,
namespace_alarms: &'namespace str,
namespace_index: &'namespace str,
namespace_alarms: &'static str,
namespace_index: &'static str,
) -> Self {
Self {
storage,
Expand All @@ -67,22 +64,22 @@ impl<'storage, 'namespace> Alarms<'storage, 'namespace> {
}
}

impl<'storage, 'namespace> AlarmsSelection for Alarms<'storage, 'namespace> {
impl<'storage> AlarmsSelection for Alarms<'storage> {
fn alarms_selection(&self, ctime: Timestamp) -> AlarmsSelectionIterator<'_> {
alarms_selection(self.storage, &self.alarms, as_seconds(ctime))
}
}

pub struct AlarmsMut<'storage, 'namespace> {
pub struct AlarmsMut<'storage> {
storage: &'storage mut dyn Storage,
alarms: IndexedMap<'namespace>,
alarms: IndexedMap,
}

impl<'storage, 'namespace> AlarmsMut<'storage, 'namespace> {
impl<'storage> AlarmsMut<'storage> {
pub fn new(
storage: &'storage mut dyn Storage,
namespace_alarms: &'namespace str,
namespace_index: &'namespace str,
namespace_alarms: &'static str,
namespace_index: &'static str,
) -> Self {
Self {
storage,
Expand Down Expand Up @@ -128,15 +125,15 @@ impl<'storage, 'namespace> AlarmsMut<'storage, 'namespace> {
}
}

impl<'storage, 'namespace> AlarmsSelection for AlarmsMut<'storage, 'namespace> {
impl<'storage> AlarmsSelection for AlarmsMut<'storage> {
fn alarms_selection(&self, ctime: Timestamp) -> AlarmsSelectionIterator<'_> {
alarms_selection(self.storage, &self.alarms, as_seconds(ctime))
}
}

fn alarms_selection<'storage>(
storage: &'storage dyn Storage,
alarms: &IndexedMap<'_>,
alarms: &IndexedMap,
time: TimeSeconds,
) -> AlarmsSelectionIterator<'storage> {
alarms
Expand Down
24 changes: 12 additions & 12 deletions packages/time-oracle/src/migrate_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,29 @@ struct AlarmOld {
pub addr: Addr,
}

struct AlarmIndexes<'a> {
alarms: MultiIndex<'a, TimeSeconds, AlarmOld, Id>,
struct AlarmIndexes {
alarms: MultiIndex<'static, TimeSeconds, AlarmOld, Id>,
}

impl<'a> IndexList<AlarmOld> for AlarmIndexes<'a> {
impl IndexList<AlarmOld> for AlarmIndexes {
fn get_indexes(&self) -> Box<dyn Iterator<Item = &'_ dyn Index<AlarmOld>> + '_> {
let v: Vec<&dyn Index<AlarmOld>> = vec![&self.alarms];

Box::new(v.into_iter())
}
}

pub struct AlarmsOld<'a> {
namespace_alarms: &'a str,
namespace_index: &'a str,
next_id: Item<'a, Id>,
pub struct AlarmsOld {
namespace_alarms: &'static str,
namespace_index: &'static str,
next_id: Item<'static, Id>,
}

impl<'a> AlarmsOld<'a> {
impl AlarmsOld {
pub const fn new(
namespace_alarms: &'a str,
namespace_index: &'a str,
namespace_next_id: &'a str,
namespace_alarms: &'static str,
namespace_index: &'static str,
namespace_next_id: &'static str,
) -> Self {
Self {
namespace_alarms,
Expand Down Expand Up @@ -114,7 +114,7 @@ impl<'a> AlarmsOld<'a> {
Ok(())
}

fn alarms(&self) -> IndexedMap<'a, TimeSeconds, AlarmOld, AlarmIndexes<'a>> {
fn alarms(&self) -> IndexedMap<'static, TimeSeconds, AlarmOld, AlarmIndexes> {
let indexes = AlarmIndexes {
alarms: MultiIndex::new(|_, d| d.time, self.namespace_alarms, self.namespace_index),
};
Expand Down

0 comments on commit 30e8452

Please sign in to comment.