Skip to content

Commit a40b8d3

Browse files
committed
Invert the implementation of JoinedValue
Signed-off-by: Michael X. Grey <[email protected]>
1 parent a6bf08d commit a40b8d3

File tree

6 files changed

+343
-318
lines changed

6 files changed

+343
-318
lines changed

src/buffer.rs

+8
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,14 @@ impl<T> Buffer<T> {
113113
pub fn location(&self) -> BufferLocation {
114114
self.location
115115
}
116+
117+
/// Cast this into an [`AnyBuffer`].
118+
pub fn as_any_buffer(&self) -> AnyBuffer
119+
where
120+
T: 'static + Send + Sync,
121+
{
122+
self.clone().into()
123+
}
116124
}
117125

118126
impl<T> Clone for Buffer<T> {

src/buffer/any_buffer.rs

+30-26
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ impl AnyBuffer {
6464
self.interface.message_type_id()
6565
}
6666

67+
pub fn message_type_name(&self) -> &'static str {
68+
self.interface.message_type_name()
69+
}
70+
6771
/// Get the [`AnyBufferAccessInterface`] for this specific instance of [`AnyBuffer`].
6872
pub fn get_interface(&self) -> &'static (dyn AnyBufferAccessInterface + Send + Sync) {
6973
self.interface
@@ -176,11 +180,11 @@ impl AnyBufferKey {
176180
self.tag.session
177181
}
178182

179-
fn is_in_use(&self) -> bool {
183+
pub(crate) fn is_in_use(&self) -> bool {
180184
self.tag.is_in_use()
181185
}
182186

183-
fn deep_clone(&self) -> Self {
187+
pub(crate) fn deep_clone(&self) -> Self {
184188
Self {
185189
tag: self.tag.deep_clone(),
186190
interface: self.interface,
@@ -338,14 +342,14 @@ impl<'w, 's, 'a> AnyBufferMut<'w, 's, 'a> {
338342
}
339343

340344
/// Pull the oldest message from the buffer.
341-
pub fn pull(&mut self) -> Option<AnyMessage> {
345+
pub fn pull(&mut self) -> Option<AnyMessageBox> {
342346
self.modified = true;
343347
self.storage.any_pull(self.session)
344348
}
345349

346350
/// Pull the message that was most recently put into the buffer (instead of the
347351
/// oldest, which is what [`Self::pull`] gives).
348-
pub fn pull_newest(&mut self) -> Option<AnyMessage> {
352+
pub fn pull_newest(&mut self) -> Option<AnyMessageBox> {
349353
self.modified = true;
350354
self.storage.any_pull_newest(self.session)
351355
}
@@ -385,7 +389,7 @@ impl<'w, 's, 'a> AnyBufferMut<'w, 's, 'a> {
385389
/// If the input value does not match the message type of the buffer, this
386390
/// will return [`Err`] and give back an error with the message that you
387391
/// tried to push and the type information for the expected message type.
388-
pub fn push_any(&mut self, value: AnyMessage) -> Result<Option<AnyMessage>, AnyMessageError> {
392+
pub fn push_any(&mut self, value: AnyMessageBox) -> Result<Option<AnyMessageBox>, AnyMessageError> {
389393
self.storage.any_push(self.session, value)
390394
}
391395

@@ -420,8 +424,8 @@ impl<'w, 's, 'a> AnyBufferMut<'w, 's, 'a> {
420424
/// The result follows the same rules as [`Self::push_any`].
421425
pub fn push_any_as_oldest(
422426
&mut self,
423-
value: AnyMessage,
424-
) -> Result<Option<AnyMessage>, AnyMessageError> {
427+
value: AnyMessageBox,
428+
) -> Result<Option<AnyMessageBox>, AnyMessageError> {
425429
self.storage.any_push_as_oldest(self.session, value)
426430
}
427431

@@ -520,10 +524,10 @@ trait AnyBufferViewing {
520524
}
521525

522526
trait AnyBufferManagement: AnyBufferViewing {
523-
fn any_push(&mut self, session: Entity, value: AnyMessage) -> AnyMessagePushResult;
524-
fn any_push_as_oldest(&mut self, session: Entity, value: AnyMessage) -> AnyMessagePushResult;
525-
fn any_pull(&mut self, session: Entity) -> Option<AnyMessage>;
526-
fn any_pull_newest(&mut self, session: Entity) -> Option<AnyMessage>;
527+
fn any_push(&mut self, session: Entity, value: AnyMessageBox) -> AnyMessagePushResult;
528+
fn any_push_as_oldest(&mut self, session: Entity, value: AnyMessageBox) -> AnyMessagePushResult;
529+
fn any_pull(&mut self, session: Entity) -> Option<AnyMessageBox>;
530+
fn any_pull_newest(&mut self, session: Entity) -> Option<AnyMessageBox>;
527531
fn any_oldest_mut<'a>(&'a mut self, session: Entity) -> Option<AnyMessageMut<'a>>;
528532
fn any_newest_mut<'a>(&'a mut self, session: Entity) -> Option<AnyMessageMut<'a>>;
529533
fn any_get_mut<'a>(&'a mut self, session: Entity, index: usize) -> Option<AnyMessageMut<'a>>;
@@ -650,37 +654,37 @@ impl<T: 'static + Send + Sync + Any> AnyBufferViewing for Mut<'_, BufferStorage<
650654

651655
pub type AnyMessageMut<'a> = &'a mut (dyn Any + 'static + Send + Sync);
652656

653-
pub type AnyMessage = Box<dyn Any + 'static + Send + Sync>;
657+
pub type AnyMessageBox = Box<dyn Any + 'static + Send + Sync>;
654658

655659
#[derive(ThisError, Debug)]
656660
#[error("failed to convert a message")]
657661
pub struct AnyMessageError {
658662
/// The original value provided
659-
pub value: AnyMessage,
663+
pub value: AnyMessageBox,
660664
/// The ID of the type expected by the buffer
661665
pub type_id: TypeId,
662666
/// The name of the type expected by the buffer
663667
pub type_name: &'static str,
664668
}
665669

666-
pub type AnyMessagePushResult = Result<Option<AnyMessage>, AnyMessageError>;
670+
pub type AnyMessagePushResult = Result<Option<AnyMessageBox>, AnyMessageError>;
667671

668672
impl<T: 'static + Send + Sync + Any> AnyBufferManagement for Mut<'_, BufferStorage<T>> {
669-
fn any_push(&mut self, session: Entity, value: AnyMessage) -> AnyMessagePushResult {
673+
fn any_push(&mut self, session: Entity, value: AnyMessageBox) -> AnyMessagePushResult {
670674
let value = from_any_message::<T>(value)?;
671675
Ok(self.push(session, value).map(to_any_message))
672676
}
673677

674-
fn any_push_as_oldest(&mut self, session: Entity, value: AnyMessage) -> AnyMessagePushResult {
678+
fn any_push_as_oldest(&mut self, session: Entity, value: AnyMessageBox) -> AnyMessagePushResult {
675679
let value = from_any_message::<T>(value)?;
676680
Ok(self.push_as_oldest(session, value).map(to_any_message))
677681
}
678682

679-
fn any_pull(&mut self, session: Entity) -> Option<AnyMessage> {
683+
fn any_pull(&mut self, session: Entity) -> Option<AnyMessageBox> {
680684
self.pull(session).map(to_any_message)
681685
}
682686

683-
fn any_pull_newest(&mut self, session: Entity) -> Option<AnyMessage> {
687+
fn any_pull_newest(&mut self, session: Entity) -> Option<AnyMessageBox> {
684688
self.pull_newest(session).map(to_any_message)
685689
}
686690

@@ -713,11 +717,11 @@ fn to_any_mut<'a, T: 'static + Send + Sync + Any>(x: &'a mut T) -> AnyMessageMut
713717
x
714718
}
715719

716-
fn to_any_message<T: 'static + Send + Sync + Any>(x: T) -> AnyMessage {
720+
fn to_any_message<T: 'static + Send + Sync + Any>(x: T) -> AnyMessageBox {
717721
Box::new(x)
718722
}
719723

720-
fn from_any_message<T: 'static + Send + Sync + Any>(value: AnyMessage) -> Result<T, AnyMessageError>
724+
fn from_any_message<T: 'static + Send + Sync + Any>(value: AnyMessageBox) -> Result<T, AnyMessageError>
721725
where
722726
T: 'static,
723727
{
@@ -799,7 +803,7 @@ pub trait AnyBufferAccessInterface {
799803
&self,
800804
entity_mut: &mut EntityWorldMut,
801805
session: Entity,
802-
) -> Result<AnyMessage, OperationError>;
806+
) -> Result<AnyMessageBox, OperationError>;
803807

804808
fn create_any_buffer_view<'a>(
805809
&self,
@@ -915,7 +919,7 @@ impl<T: 'static + Send + Sync + Any> AnyBufferAccessInterface for AnyBufferAcces
915919
&self,
916920
entity_mut: &mut EntityWorldMut,
917921
session: Entity,
918-
) -> Result<AnyMessage, OperationError> {
922+
) -> Result<AnyMessageBox, OperationError> {
919923
entity_mut
920924
.pull_from_buffer::<T>(session)
921925
.map(to_any_message)
@@ -955,19 +959,19 @@ pub struct DrainAnyBuffer<'a> {
955959
}
956960

957961
impl<'a> Iterator for DrainAnyBuffer<'a> {
958-
type Item = AnyMessage;
962+
type Item = AnyMessageBox;
959963

960964
fn next(&mut self) -> Option<Self::Item> {
961965
self.interface.any_next()
962966
}
963967
}
964968

965969
trait DrainAnyBufferInterface {
966-
fn any_next(&mut self) -> Option<AnyMessage>;
970+
fn any_next(&mut self) -> Option<AnyMessageBox>;
967971
}
968972

969973
impl<T: 'static + Send + Sync + Any> DrainAnyBufferInterface for DrainBuffer<'_, T> {
970-
fn any_next(&mut self) -> Option<AnyMessage> {
974+
fn any_next(&mut self) -> Option<AnyMessageBox> {
971975
self.next().map(to_any_message)
972976
}
973977
}
@@ -1015,7 +1019,7 @@ impl Buffered for AnyBuffer {
10151019
}
10161020

10171021
impl Joined for AnyBuffer {
1018-
type Item = AnyMessage;
1022+
type Item = AnyMessageBox;
10191023
fn pull(&self, session: Entity, world: &mut World) -> Result<Self::Item, OperationError> {
10201024
let mut buffer_mut = world.get_entity_mut(self.id()).or_broken()?;
10211025
self.interface.pull(&mut buffer_mut, session)

0 commit comments

Comments
 (0)