Fix names and types misrepresenting PduCount as ShortEventId.
Add get_shorteventid_from_pdu_id() conversion. Fix prev/next nearest-state interface (dev branch 642086ecfcfa). Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
@@ -6,6 +6,11 @@ pub mod room_version;
|
||||
pub mod state_res;
|
||||
|
||||
pub use event::{Event, StateKey, TypeExt as EventTypeExt, TypeStateKey, state_key};
|
||||
pub use pdu::{EventHash, Pdu, PduBuilder, PduCount, PduEvent, PduId, RawPduId, ShortId};
|
||||
pub use pdu::{EventHash, Pdu, PduBuilder, PduCount, PduEvent, PduId, RawPduId};
|
||||
pub use room_version::{RoomVersion, RoomVersionRules};
|
||||
pub use state_res::{StateMap, events};
|
||||
|
||||
pub type ShortStateKey = ShortId;
|
||||
pub type ShortEventId = ShortId;
|
||||
pub type ShortRoomId = ShortId;
|
||||
pub type ShortId = u64;
|
||||
|
||||
@@ -24,10 +24,10 @@ pub use self::{
|
||||
builder::{Builder, Builder as PduBuilder},
|
||||
count::Count,
|
||||
hashes::EventHashes as EventHash,
|
||||
id::{ShortId, *},
|
||||
id::Id,
|
||||
raw_id::*,
|
||||
};
|
||||
use super::{Event, StateKey};
|
||||
use super::{Event, ShortRoomId, StateKey};
|
||||
use crate::Result;
|
||||
|
||||
/// Persistent Data Unit (Event)
|
||||
|
||||
@@ -17,6 +17,10 @@ pub enum Count {
|
||||
}
|
||||
|
||||
impl Count {
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn to_be_bytes(self) -> [u8; size_of::<u64>()] { self.into_unsigned().to_be_bytes() }
|
||||
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn from_unsigned(unsigned: u64) -> Self { Self::from_signed(unsigned as i64) }
|
||||
|
||||
@@ -1,14 +1,9 @@
|
||||
use super::{Count, RawId};
|
||||
|
||||
pub type ShortRoomId = ShortId;
|
||||
pub type ShortEventId = ShortId;
|
||||
pub type ShortStateKey = ShortId;
|
||||
pub type ShortId = u64;
|
||||
use super::{Count, RawId, ShortRoomId};
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub struct Id {
|
||||
pub shortroomid: ShortRoomId,
|
||||
pub shorteventid: Count,
|
||||
pub count: Count,
|
||||
}
|
||||
|
||||
impl From<RawId> for Id {
|
||||
@@ -16,7 +11,7 @@ impl From<RawId> for Id {
|
||||
fn from(raw: RawId) -> Self {
|
||||
Self {
|
||||
shortroomid: u64::from_be_bytes(raw.shortroomid()),
|
||||
shorteventid: Count::from_unsigned(u64::from_be_bytes(raw.shorteventid())),
|
||||
count: Count::from_unsigned(u64::from_be_bytes(raw.count())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,10 @@ use std::fmt;
|
||||
use arrayvec::ArrayVec;
|
||||
use serde::{Deserialize, Deserializer};
|
||||
|
||||
use super::{Count, Id, ShortEventId, ShortId, ShortRoomId};
|
||||
use super::{
|
||||
super::{ShortId, ShortRoomId},
|
||||
Count, Id,
|
||||
};
|
||||
|
||||
#[derive(Clone, Copy, Eq, Hash, PartialEq)]
|
||||
pub enum RawId {
|
||||
@@ -19,9 +22,9 @@ struct RawIdVisitor;
|
||||
const INT_LEN: usize = size_of::<ShortId>();
|
||||
|
||||
impl RawId {
|
||||
const BACKFILLED_LEN: usize = size_of::<ShortRoomId>() + INT_LEN + size_of::<ShortEventId>();
|
||||
const BACKFILLED_LEN: usize = size_of::<ShortRoomId>() + INT_LEN + size_of::<i64>();
|
||||
const MAX_LEN: usize = Self::BACKFILLED_LEN;
|
||||
const NORMAL_LEN: usize = size_of::<ShortRoomId>() + size_of::<ShortEventId>();
|
||||
const NORMAL_LEN: usize = size_of::<ShortRoomId>() + size_of::<u64>();
|
||||
|
||||
#[inline]
|
||||
#[must_use]
|
||||
@@ -29,9 +32,9 @@ impl RawId {
|
||||
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn pdu_count(&self) -> Count {
|
||||
let id: Id = (*self).into();
|
||||
id.shorteventid
|
||||
pub fn pdu_count(self) -> Count {
|
||||
let id: Id = self.into();
|
||||
id.count
|
||||
}
|
||||
|
||||
#[inline]
|
||||
@@ -49,14 +52,14 @@ impl RawId {
|
||||
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn shorteventid(self) -> [u8; INT_LEN] {
|
||||
pub fn count(self) -> [u8; INT_LEN] {
|
||||
match self {
|
||||
| Self::Normal(raw) => raw[INT_LEN..INT_LEN * 2]
|
||||
.try_into()
|
||||
.expect("normal raw shorteventid array from slice"),
|
||||
.expect("normal raw indice array from slice"),
|
||||
| Self::Backfilled(raw) => raw[INT_LEN * 2..INT_LEN * 3]
|
||||
.try_into()
|
||||
.expect("backfilled raw shorteventid array from slice"),
|
||||
.expect("backfilled raw indice array from slice"),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,19 +106,19 @@ impl From<Id> for RawId {
|
||||
|
||||
let mut vec = RawVec::new();
|
||||
vec.extend(id.shortroomid.to_be_bytes());
|
||||
id.shorteventid.debug_assert_valid();
|
||||
match id.shorteventid {
|
||||
| Count::Normal(shorteventid) => {
|
||||
vec.extend(shorteventid.to_be_bytes());
|
||||
id.count.debug_assert_valid();
|
||||
match id.count {
|
||||
| Count::Normal(count) => {
|
||||
vec.extend(count.to_be_bytes());
|
||||
Self::Normal(
|
||||
vec.as_ref()
|
||||
.try_into()
|
||||
.expect("RawVec into RawId::Normal"),
|
||||
)
|
||||
},
|
||||
| Count::Backfilled(shorteventid) => {
|
||||
| Count::Backfilled(count) => {
|
||||
vec.extend(0_u64.to_be_bytes());
|
||||
vec.extend(shorteventid.to_be_bytes());
|
||||
vec.extend(count.to_be_bytes());
|
||||
Self::Backfilled(
|
||||
vec.as_ref()
|
||||
.try_into()
|
||||
|
||||
Reference in New Issue
Block a user