Remove lifetime mess, replace a lot of code with Arc instead of borrows.
Some checks failed
continuous-integration/drone/push Build is failing

This cleans up the codebase massively, and allows me to maintain some semblance of sanity.
This commit is contained in:
2022-08-20 13:17:20 +02:00
parent 2d4253e155
commit 55cc0906c9
34 changed files with 320 additions and 366 deletions

View File

@@ -10,9 +10,9 @@ use crate::dynamic_data::{ScriptSource, ScriptSourceData};
/// The data on a turn choice that should be contained in every turn choice, regardless of type.
#[derive(Debug)]
struct CommonChoiceData<'user, 'library> {
struct CommonChoiceData {
/// The user of the turn choice
user: Arc<Pokemon<'user, 'library>>,
user: Arc<Pokemon>,
/// The speed of the user at the beginning of the turn.
speed: u32,
/// This random value is set at the beginning of the turn. It is used for tie breaking of the
@@ -30,22 +30,22 @@ struct CommonChoiceData<'user, 'library> {
/// This enum defines a single choice for a Pokemon for a battle turn.
#[derive(Debug)]
#[cfg_attr(feature = "wasm", derive(unique_type_id_derive::UniqueTypeId))]
pub enum TurnChoice<'user, 'library> {
pub enum TurnChoice {
/// A move choice tells a Pokemon to use a move on a target for this turn.
Move(MoveChoice<'user, 'library>),
Move(MoveChoice),
/// An item choice tells a Pokemon to use an item.
Item(ItemChoice<'user, 'library>),
Item(ItemChoice),
/// A switch choice tells a Pokemon to switch with another Pokemon from the party.
Switch(SwitchChoice<'user, 'library>),
Switch(SwitchChoice),
/// A flee choice tells a Pokemon to flee from battle.
Flee(FleeChoice<'user, 'library>),
Flee(FleeChoice),
/// A pass choice tells the user to do nothing that turn.
Pass(PassChoice<'user, 'library>),
Pass(PassChoice),
}
impl<'user, 'library> TurnChoice<'user, 'library> {
impl TurnChoice {
/// The shared choice data between each of the different turn choices.
fn choice_data(&self) -> &CommonChoiceData<'user, 'library> {
fn choice_data(&self) -> &CommonChoiceData {
match self {
TurnChoice::Move(data) => &data.choice_data,
TurnChoice::Item(data) => &data.choice_data,
@@ -55,7 +55,7 @@ impl<'user, 'library> TurnChoice<'user, 'library> {
}
}
/// The shared choice data between each of the different turn choices.
fn choice_data_mut(&mut self) -> &mut Box<CommonChoiceData<'user, 'library>> {
fn choice_data_mut(&mut self) -> &mut Box<CommonChoiceData> {
match self {
TurnChoice::Move(data) => &mut data.choice_data,
TurnChoice::Item(data) => &mut data.choice_data,
@@ -66,7 +66,7 @@ impl<'user, 'library> TurnChoice<'user, 'library> {
}
/// Get the user of the given choice.
pub fn user(&self) -> &Arc<Pokemon<'user, 'library>> {
pub fn user(&self) -> &Arc<Pokemon> {
&self.choice_data().user
}
@@ -108,7 +108,7 @@ impl<'user, 'library> TurnChoice<'user, 'library> {
/// Helper function to get the move choice data from a turn. Note that this will panic if not
/// used on a move choice.
pub(crate) fn get_move_turn_data<'b>(&'b self) -> &'b MoveChoice<'user, 'library> {
pub(crate) fn get_move_turn_data(&self) -> &MoveChoice {
if let TurnChoice::Move(data) = self {
return data;
}
@@ -116,7 +116,7 @@ impl<'user, 'library> TurnChoice<'user, 'library> {
}
}
impl<'user, 'library> ScriptSource<'user> for TurnChoice<'user, 'library> {
impl ScriptSource for TurnChoice {
fn get_script_count(&self) -> usize {
match self {
TurnChoice::Move(data) => data.get_script_count(),
@@ -160,9 +160,9 @@ impl<'user, 'library> ScriptSource<'user> for TurnChoice<'user, 'library> {
/// The data attached to a move choice.
#[derive(Debug)]
pub struct MoveChoice<'user, 'library> {
pub struct MoveChoice {
/// The move that is used for this choice.
used_move: Arc<LearnedMove<'library>>,
used_move: Arc<LearnedMove>,
/// The side this move is aimed at.
target_side: u8,
/// The index of the Pokemon on the side we're aiming at.
@@ -172,17 +172,12 @@ pub struct MoveChoice<'user, 'library> {
/// The priority of the move choice at the beginning of the turn.
priority: i8,
/// The common turn choice data.
choice_data: Box<CommonChoiceData<'user, 'library>>,
choice_data: Box<CommonChoiceData>,
}
impl<'user, 'library> MoveChoice<'user, 'library> {
impl<'user, 'library> MoveChoice {
/// Initializes the data for a new move choice.
pub fn new(
user: Arc<Pokemon<'user, 'library>>,
used_move: Arc<LearnedMove<'library>>,
target_side: u8,
target_index: u8,
) -> Self {
pub fn new(user: Arc<Pokemon>, used_move: Arc<LearnedMove>, target_side: u8, target_index: u8) -> Self {
Self {
used_move,
target_side,
@@ -200,7 +195,7 @@ impl<'user, 'library> MoveChoice<'user, 'library> {
}
/// The actual learned move on the Pokemon we use for this choice.
pub fn used_move(&self) -> &Arc<LearnedMove<'library>> {
pub fn used_move(&self) -> &Arc<LearnedMove> {
&self.used_move
}
@@ -221,7 +216,7 @@ impl<'user, 'library> MoveChoice<'user, 'library> {
&mut self.priority
}
/// The user of the choice.
pub fn user(&self) -> &Arc<Pokemon<'user, 'library>> {
pub fn user(&self) -> &Arc<Pokemon> {
&self.choice_data.user
}
/// The move script of the choice.
@@ -230,7 +225,7 @@ impl<'user, 'library> MoveChoice<'user, 'library> {
}
}
impl<'user, 'library> ScriptSource<'user> for MoveChoice<'user, 'library> {
impl ScriptSource for MoveChoice {
fn get_script_count(&self) -> usize {
1 + self.choice_data.user.get_script_count()
}
@@ -251,14 +246,14 @@ impl<'user, 'library> ScriptSource<'user> for MoveChoice<'user, 'library> {
/// The data given when we select an item choice.
#[derive(Debug)]
pub struct ItemChoice<'user, 'library> {
pub struct ItemChoice {
/// The shared data of all turn choices.
choice_data: Box<CommonChoiceData<'user, 'library>>,
choice_data: Box<CommonChoiceData>,
}
impl<'user, 'library> ItemChoice<'user, 'library> {
impl ItemChoice {
/// Initialised a new item choice.
pub fn new(user: Arc<Pokemon<'user, 'library>>) -> Self {
pub fn new(user: Arc<Pokemon>) -> Self {
Self {
choice_data: Box::new(CommonChoiceData {
user,
@@ -271,7 +266,7 @@ impl<'user, 'library> ItemChoice<'user, 'library> {
}
}
impl<'user, 'library> ScriptSource<'user> for ItemChoice<'user, 'library> {
impl ScriptSource for ItemChoice {
fn get_script_count(&self) -> usize {
0
}
@@ -289,14 +284,14 @@ impl<'user, 'library> ScriptSource<'user> for ItemChoice<'user, 'library> {
/// The data given when we select a switch choice.
#[derive(Debug)]
pub struct SwitchChoice<'user, 'library> {
pub struct SwitchChoice {
/// The shared data of all turn choices.
choice_data: Box<CommonChoiceData<'user, 'library>>,
choice_data: Box<CommonChoiceData>,
}
impl<'user, 'library> SwitchChoice<'user, 'library> {
impl SwitchChoice {
/// Initialise the turn choice data.
pub fn new(user: Arc<Pokemon<'user, 'library>>) -> Self {
pub fn new(user: Arc<Pokemon>) -> Self {
Self {
choice_data: Box::new(CommonChoiceData {
user,
@@ -309,7 +304,7 @@ impl<'user, 'library> SwitchChoice<'user, 'library> {
}
}
impl<'user, 'library> ScriptSource<'user> for SwitchChoice<'user, 'library> {
impl ScriptSource for SwitchChoice {
fn get_script_count(&self) -> usize {
0
}
@@ -327,14 +322,14 @@ impl<'user, 'library> ScriptSource<'user> for SwitchChoice<'user, 'library> {
/// The data given when we select a flee choice.
#[derive(Debug)]
pub struct FleeChoice<'user, 'library> {
pub struct FleeChoice {
/// The common data all turn choices share.
choice_data: Box<CommonChoiceData<'user, 'library>>,
choice_data: Box<CommonChoiceData>,
}
impl<'user, 'library> FleeChoice<'user, 'library> {
impl FleeChoice {
/// Initialises a new flee choice.
pub fn new(user: Arc<Pokemon<'user, 'library>>) -> Self {
pub fn new(user: Arc<Pokemon>) -> Self {
Self {
choice_data: Box::new(CommonChoiceData {
user,
@@ -347,7 +342,7 @@ impl<'user, 'library> FleeChoice<'user, 'library> {
}
}
impl<'user, 'library> ScriptSource<'user> for FleeChoice<'user, 'library> {
impl ScriptSource for FleeChoice {
fn get_script_count(&self) -> usize {
0
}
@@ -365,14 +360,14 @@ impl<'user, 'library> ScriptSource<'user> for FleeChoice<'user, 'library> {
/// The data given when we select a pass choice.
#[derive(Debug)]
pub struct PassChoice<'user, 'library> {
pub struct PassChoice {
/// The common data of all turn choices.
choice_data: Box<CommonChoiceData<'user, 'library>>,
choice_data: Box<CommonChoiceData>,
}
impl<'user, 'library> PassChoice<'user, 'library> {
impl PassChoice {
/// Initialised a new pass choice.
pub fn new(user: Arc<Pokemon<'user, 'library>>) -> Self {
pub fn new(user: Arc<Pokemon>) -> Self {
Self {
choice_data: Box::new(CommonChoiceData {
user,
@@ -385,7 +380,7 @@ impl<'user, 'library> PassChoice<'user, 'library> {
}
}
impl<'user, 'library> ScriptSource<'user> for PassChoice<'user, 'library> {
impl ScriptSource for PassChoice {
fn get_script_count(&self) -> usize {
0
}
@@ -401,21 +396,21 @@ impl<'user, 'library> ScriptSource<'user> for PassChoice<'user, 'library> {
}
}
impl<'user, 'library> PartialEq<Self> for TurnChoice<'user, 'library> {
impl PartialEq<Self> for TurnChoice {
fn eq(&self, other: &Self) -> bool {
std::ptr::eq(self, other)
}
}
impl<'user, 'library> Eq for TurnChoice<'user, 'library> {}
impl Eq for TurnChoice {}
impl<'user, 'library> PartialOrd for TurnChoice<'user, 'library> {
impl PartialOrd for TurnChoice {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl<'user, 'library> Ord for TurnChoice<'user, 'library> {
impl Ord for TurnChoice {
fn cmp(&self, other: &Self) -> Ordering {
match self {
TurnChoice::Move(data) => {