Skip to content

Commit

Permalink
Moving the master account to the heap to avoid stack overflow.
Browse files Browse the repository at this point in the history
  • Loading branch information
alensiljak committed Sep 7, 2023
1 parent 47a90e5 commit 2697b18
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 18 deletions.
10 changes: 5 additions & 5 deletions src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ mod tests {
let ptr = journal.master.find_account("Assets").unwrap();
let assets = journal.get_account(ptr);

assert_eq!(addr_of!(journal.master), assets.parent);
assert_eq!(&*journal.master as *const Account, assets.parent);
}

#[test]
Expand All @@ -338,7 +338,7 @@ mod tests {
let ptr = journal.master.find_account("Assets").unwrap();
let assets = journal.get_account(ptr);

assert_eq!(&journal.master as *const Account, assets.parent);
assert_eq!(&*journal.master as *const Account, assets.parent);

// test fullname
let assets_fullname = journal.master.accounts.get("Assets").unwrap().fullname();
Expand All @@ -351,7 +351,7 @@ mod tests {
let ptr = journal.master.find_account("Assets").unwrap();
let assets = journal.get_account(ptr);

assert_eq!(addr_of!(journal.master), assets.parent);
assert_eq!(&*journal.master as *const Account, assets.parent);
}

#[test_log::test]
Expand All @@ -366,15 +366,15 @@ mod tests {

// expenses
let expenses = journal.master.find_account("Expenses").unwrap();
assert_eq!(addr_of!(journal.master), expenses.parent);
assert_eq!(&*journal.master as *const Account, expenses.parent);

// groceries
let groceries = expenses.find_account("Groceries").unwrap();
assert_eq!(expenses as *const Account, groceries.parent);

// assets
let assets = journal.master.find_account("Assets").unwrap();
assert_eq!(addr_of!(journal.master), assets.parent);
assert_eq!(&*journal.master as *const Account, assets.parent);

// confirm that addr_of! and `as *const` are the same.
assert_eq!(assets as *const Account, addr_of!(*assets));
Expand Down
13 changes: 4 additions & 9 deletions src/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,7 @@ fn get_latest_price(
return None;
}

// let mut dates: Vec<&NaiveDateTime> = prices.keys().collect();
// dates.sort();
// let last_date = *dates.last().unwrap();
// prices.get(last_date)

// BTreeMap does this for us.
// BTreeMap orders by key (date) by default.
prices.last_key_value()
}

Expand Down Expand Up @@ -359,13 +354,13 @@ mod tests {
prices.insert(parse_datetime("2023-05-02").unwrap(), Quantity::from(40));

// act
let Some((actual_date, actual_quantity)) = get_latest_price(&prices) else {
let Some((&actual_date, &actual_quantity)) = get_latest_price(&prices) else {
panic!("Should not happen!")
};

// assert!(actual.is_some());
assert_eq!(newest_date, *actual_date);
assert_eq!(Quantity::from(30), *actual_quantity);
assert_eq!(newest_date, actual_date);
assert_eq!(Quantity::from(30), actual_quantity);
}

#[test]
Expand Down
8 changes: 4 additions & 4 deletions src/journal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{
// pub type XactIndex = usize;

pub struct Journal {
pub master: Account,
pub master: Box<Account>,

pub commodity_pool: CommodityPool,
pub xacts: Vec<Xact>,
Expand All @@ -27,7 +27,7 @@ pub struct Journal {
impl Journal {
pub fn new() -> Self {
Self {
master: Account::new(""),
master: Box::new(Account::new("")),

commodity_pool: CommodityPool::new(),
xacts: vec![],
Expand Down Expand Up @@ -132,7 +132,7 @@ mod tests {
let Some(ptr) = journal.register_account(NAME) else {panic!("unexpected")};
let actual = journal.get_account(ptr);

assert_eq!(&journal.master as *const Account, actual.parent);
assert_eq!(&*journal.master as *const Account, actual.parent);
}

#[test]
Expand Down Expand Up @@ -167,7 +167,7 @@ mod tests {
let assets = master.find_account("Assets").unwrap();
// let assets = journal.get_account(assets_ptr);
assert_eq!("Assets", assets.name);
assert_eq!(addr_of!(journal.master), assets.parent);
assert_eq!(&*journal.master as *const Account, assets.parent);

let inv = assets.find_account("Investments").unwrap();
// let inv = journal.get_account_mut(inv_ix);
Expand Down

0 comments on commit 2697b18

Please sign in to comment.