mod.rs 854 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. use parking_lot::Mutex;
  2. struct State {
  3. bytes: bytes::Bytes,
  4. snapshot: Vec<u8>,
  5. }
  6. pub struct Persister {
  7. state: Mutex<State>,
  8. }
  9. impl Persister {
  10. pub fn new() -> Self {
  11. Self {
  12. state: Mutex::new(State {
  13. bytes: bytes::Bytes::new(),
  14. snapshot: vec![],
  15. }),
  16. }
  17. }
  18. }
  19. impl ruaft::Persister for Persister {
  20. fn read_state(&self) -> bytes::Bytes {
  21. self.state.lock().bytes.clone()
  22. }
  23. fn save_state(&self, data: bytes::Bytes) {
  24. self.state.lock().bytes = data;
  25. }
  26. fn state_size(&self) -> usize {
  27. self.state.lock().bytes.len()
  28. }
  29. fn save_snapshot_and_state(&self, state: bytes::Bytes, snapshot: &[u8]) {
  30. let mut this = self.state.lock();
  31. this.bytes = state;
  32. this.snapshot = snapshot.to_vec();
  33. }
  34. }