bumpframe.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. use crate::innerlude::*;
  2. use bumpalo::Bump;
  3. use std::cell::Cell;
  4. pub(crate) struct ActiveFrame {
  5. // We use a "generation" for users of contents in the bump frames to ensure their data isn't broken
  6. pub generation: Cell<usize>,
  7. // The double-buffering situation that we will use
  8. pub frames: [BumpFrame; 2],
  9. }
  10. pub(crate) struct BumpFrame {
  11. pub bump: Bump,
  12. pub(crate) head_node: VNode<'static>,
  13. #[cfg(test)]
  14. // used internally for debugging
  15. _name: &'static str,
  16. }
  17. impl ActiveFrame {
  18. pub fn new() -> Self {
  19. let frame_a = BumpFrame {
  20. bump: Bump::new(),
  21. head_node: NodeFactory::unstable_place_holder(),
  22. #[cfg(test)]
  23. _name: "wip",
  24. };
  25. let frame_b = BumpFrame {
  26. bump: Bump::new(),
  27. head_node: NodeFactory::unstable_place_holder(),
  28. #[cfg(test)]
  29. _name: "fin",
  30. };
  31. Self {
  32. generation: 0.into(),
  33. frames: [frame_a, frame_b],
  34. }
  35. }
  36. pub unsafe fn reset_wip_frame(&mut self) {
  37. self.wip_frame_mut().bump.reset()
  38. }
  39. /// The "work in progress frame" represents the frame that is currently being worked on.
  40. pub fn wip_frame(&self) -> &BumpFrame {
  41. match self.generation.get() & 1 == 0 {
  42. true => &self.frames[0],
  43. false => &self.frames[1],
  44. }
  45. }
  46. pub fn wip_frame_mut(&mut self) -> &mut BumpFrame {
  47. match self.generation.get() & 1 == 0 {
  48. true => &mut self.frames[0],
  49. false => &mut self.frames[1],
  50. }
  51. }
  52. /// The finished frame represents the frame that has been "finished" and cannot be modified again
  53. pub fn finished_frame(&self) -> &BumpFrame {
  54. match self.generation.get() & 1 == 1 {
  55. true => &self.frames[0],
  56. false => &self.frames[1],
  57. }
  58. }
  59. /// Give out our self-referential item with our own borrowed lifetime
  60. pub fn fin_head<'b>(&'b self) -> &'b VNode<'b> {
  61. let cur_head = &self.finished_frame().head_node;
  62. unsafe { std::mem::transmute::<&VNode<'static>, &VNode<'b>>(cur_head) }
  63. }
  64. /// Give out our self-referential item with our own borrowed lifetime
  65. pub fn wip_head<'b>(&'b self) -> &'b VNode<'b> {
  66. let cur_head = &self.wip_frame().head_node;
  67. unsafe { std::mem::transmute::<&VNode<'static>, &VNode<'b>>(cur_head) }
  68. }
  69. pub fn cycle_frame(&mut self) {
  70. self.generation.set(self.generation.get() + 1);
  71. }
  72. }
  73. #[cfg(test)]
  74. mod tests {
  75. //! These tests are bad. I don't have a good way of properly testing the ActiveFrame stuff
  76. use super::*;
  77. #[test]
  78. fn test_bump_frame() {
  79. let mut frames = ActiveFrame::new();
  80. // just cycle a few times and make sure we get the right frames out
  81. for _ in 0..5 {
  82. let fin = frames.finished_frame();
  83. let wip = frames.wip_frame();
  84. assert_eq!(wip._name, "wip");
  85. assert_eq!(fin._name, "fin");
  86. frames.cycle_frame();
  87. let fin = frames.finished_frame();
  88. let wip = frames.wip_frame();
  89. assert_eq!(wip._name, "fin");
  90. assert_eq!(fin._name, "wip");
  91. frames.cycle_frame();
  92. }
  93. assert_eq!(frames.generation.get(), 10);
  94. }
  95. }