1#![forbid(unsafe_code)]
2#![cfg_attr(docsrs, feature(doc_cfg))]
3#![cfg_attr(docsrs, doc(cfg_hide(doc)))]
4
5use std::{
6 any::Any,
7 marker::PhantomData,
8 ops::{Deref, DerefMut},
9 sync::Arc,
10};
11
12use futures_util::{TryFutureExt, future::ready};
13use object_rainbow::{
14 Address, ByteNode, DefaultHash, Equivalent, ExtraFor, FailFuture, Fetch, FetchBytes, FullHash,
15 Hash, InlineOutput, ListHashes, MaybeHasNiche, Node, ObjectMarker, OptionalHash, Output, Parse,
16 ParseAsInline, ParseInline, PointInput, PointVisitor, Resolve, Singular, Size, Tagged, Tags,
17 ToOutput, Topological, Traversible,
18};
19
20#[cfg(feature = "serde")]
21mod point_deserialize;
22#[cfg(feature = "point-serialize")]
23mod point_serialize;
24
25#[derive(Clone, ParseAsInline)]
26struct Extras<Extra>(Extra);
27
28impl<Extra> Deref for Extras<Extra> {
29 type Target = Extra;
30
31 fn deref(&self) -> &Self::Target {
32 &self.0
33 }
34}
35
36impl<Extra> ToOutput for Extras<Extra> {
37 fn to_output(&self, _: &mut dyn Output) {}
38}
39
40impl<Extra> InlineOutput for Extras<Extra> {}
41
42impl<I: PointInput> ParseInline<I> for Extras<I::Extra> {
43 fn parse_inline(input: &mut I) -> object_rainbow::Result<Self> {
44 Ok(Self(input.extra().clone()))
45 }
46}
47
48impl<Extra> Tagged for Extras<Extra> {}
49impl<Extra> ListHashes for Extras<Extra> {}
50impl<Extra> Topological for Extras<Extra> {}
51
52#[derive(Clone)]
53struct ByAddressInner {
54 address: Address,
55 resolve: Arc<dyn Resolve>,
56}
57
58impl FetchBytes for ByAddressInner {
59 fn fetch_bytes(&'_ self) -> FailFuture<'_, ByteNode> {
60 self.resolve.resolve(self.address, &self.resolve)
61 }
62
63 fn fetch_data(&'_ self) -> FailFuture<'_, Vec<u8>> {
64 self.resolve.resolve_data(self.address)
65 }
66
67 fn fetch_bytes_local(&self) -> object_rainbow::Result<Option<ByteNode>> {
68 self.resolve.try_resolve_local(self.address, &self.resolve)
69 }
70
71 fn as_resolve(&self) -> Option<&Arc<dyn Resolve>> {
72 Some(&self.resolve)
73 }
74
75 fn try_unwrap_resolve(self: Arc<Self>) -> Option<Arc<dyn Resolve>> {
76 Arc::try_unwrap(self)
77 .ok()
78 .map(|Self { resolve, .. }| resolve)
79 }
80}
81
82impl Singular for ByAddressInner {
83 fn hash(&self) -> Hash {
84 self.address.hash
85 }
86}
87
88struct ByAddress<T, Extra> {
89 inner: ByAddressInner,
90 extra: Extra,
91 _object: PhantomData<fn() -> T>,
92}
93
94impl<T, Extra> ByAddress<T, Extra> {
95 fn from_inner(inner: ByAddressInner, extra: Extra) -> Self {
96 Self {
97 inner,
98 extra,
99 _object: PhantomData,
100 }
101 }
102}
103
104impl<T, Extra> FetchBytes for ByAddress<T, Extra> {
105 fn fetch_bytes(&'_ self) -> FailFuture<'_, ByteNode> {
106 self.inner.fetch_bytes()
107 }
108
109 fn fetch_data(&'_ self) -> FailFuture<'_, Vec<u8>> {
110 self.inner.fetch_data()
111 }
112
113 fn fetch_bytes_local(&self) -> object_rainbow::Result<Option<ByteNode>> {
114 self.inner.fetch_bytes_local()
115 }
116
117 fn as_inner(&self) -> Option<&dyn Any> {
118 Some(&self.inner)
119 }
120
121 fn as_resolve(&self) -> Option<&Arc<dyn Resolve>> {
122 self.inner.as_resolve()
123 }
124
125 fn try_unwrap_resolve(self: Arc<Self>) -> Option<Arc<dyn Resolve>> {
126 Arc::try_unwrap(self).ok().map(
127 |Self {
128 inner: ByAddressInner { resolve, .. },
129 ..
130 }| resolve,
131 )
132 }
133}
134
135impl<T, Extra: Send + Sync> Singular for ByAddress<T, Extra> {
136 fn hash(&self) -> Hash {
137 self.inner.hash()
138 }
139}
140
141impl<T: FullHash, Extra: Send + Sync + ExtraFor<T>> Fetch for ByAddress<T, Extra> {
142 type T = T;
143
144 fn fetch_full(&'_ self) -> FailFuture<'_, Node<Self::T>> {
145 Box::pin(async {
146 let (data, resolve) = self.fetch_bytes().await?;
147 let object = self
148 .extra
149 .parse_checked(self.inner.address.hash, &data, &resolve)?;
150 Ok((object, resolve))
151 })
152 }
153
154 fn fetch(&'_ self) -> FailFuture<'_, Self::T> {
155 Box::pin(async {
156 let (data, resolve) = self.fetch_bytes().await?;
157 self.extra
158 .parse_checked(self.inner.address.hash, &data, &resolve)
159 })
160 }
161
162 fn try_fetch_local(&self) -> object_rainbow::Result<Option<Node<Self::T>>> {
163 let Some((data, resolve)) = self.fetch_bytes_local()? else {
164 return Ok(None);
165 };
166 let object = self
167 .extra
168 .parse_checked(self.inner.address.hash, &data, &resolve)?;
169 Ok(Some((object, resolve)))
170 }
171}
172
173trait FromInner {
174 type Inner: 'static + Clone;
175 type Extra: 'static + Clone;
176
177 fn from_inner(inner: Self::Inner, extra: Self::Extra) -> Self;
178}
179
180trait InnerCast: FetchBytes {
181 fn inner_cast<T: FromInner>(&self, extra: &T::Extra) -> Option<T> {
182 self.as_inner()?
183 .downcast_ref()
184 .cloned()
185 .map(|inner| T::from_inner(inner, extra.clone()))
186 }
187}
188
189impl<T: ?Sized + FetchBytes> InnerCast for T {}
190
191pub trait ExtractResolve: FetchBytes {
192 fn extract_resolve<R: Any>(&self) -> Option<(&Address, &R)> {
193 let ByAddressInner { address, resolve } =
194 self.as_inner()?.downcast_ref::<ByAddressInner>()?;
195 let resolve = resolve.as_ref().any_ref().downcast_ref::<R>()?;
196 Some((address, resolve))
197 }
198}
199
200impl<T: ?Sized + FetchBytes> ExtractResolve for T {}
201
202#[derive(Clone, ParseAsInline)]
203pub struct RawPointInner {
204 hash: Hash,
205 fetch: Arc<dyn Send + Sync + FetchBytes>,
206}
207
208impl RawPointInner {
209 pub fn cast<T, Extra: 'static + Clone>(self, extra: Extra) -> RawPoint<T, Extra> {
210 RawPoint::from_inner(self, extra)
211 }
212
213 pub fn from_address(address: Address, resolve: Arc<dyn Resolve>) -> Self {
214 Self {
215 hash: address.hash,
216 fetch: Arc::new(ByAddressInner { address, resolve }),
217 }
218 }
219
220 pub fn from_singular(singular: impl 'static + Singular) -> Self {
221 Self {
222 hash: singular.hash(),
223 fetch: Arc::new(singular),
224 }
225 }
226}
227
228impl ToOutput for RawPointInner {
229 fn to_output(&self, output: &mut dyn Output) {
230 self.hash.to_output(output);
231 }
232}
233
234impl InlineOutput for RawPointInner {}
235
236impl<I: PointInput> ParseInline<I> for RawPointInner {
237 fn parse_inline(input: &mut I) -> object_rainbow::Result<Self> {
238 Ok(Self::from_address(input.parse_inline()?, input.resolve()))
239 }
240}
241
242impl Tagged for RawPointInner {}
243
244impl Singular for RawPointInner {
245 fn hash(&self) -> Hash {
246 self.hash
247 }
248}
249
250impl ListHashes for RawPointInner {
251 fn list_hashes(&self, f: &mut impl FnMut(Hash)) {
252 f(self.hash)
253 }
254
255 fn point_count(&self) -> usize {
256 1
257 }
258}
259
260impl FetchBytes for RawPointInner {
261 fn fetch_bytes(&'_ self) -> FailFuture<'_, ByteNode> {
262 self.fetch.fetch_bytes()
263 }
264
265 fn fetch_data(&'_ self) -> FailFuture<'_, Vec<u8>> {
266 self.fetch.fetch_data()
267 }
268
269 fn fetch_bytes_local(&self) -> object_rainbow::Result<Option<ByteNode>> {
270 self.fetch.fetch_bytes_local()
271 }
272
273 fn fetch_data_local(&self) -> Option<Vec<u8>> {
274 self.fetch.fetch_data_local()
275 }
276
277 fn as_resolve(&self) -> Option<&Arc<dyn Resolve>> {
278 self.fetch.as_resolve()
279 }
280
281 fn try_unwrap_resolve(self: Arc<Self>) -> Option<Arc<dyn Resolve>> {
282 Arc::try_unwrap(self).ok()?.fetch.try_unwrap_resolve()
283 }
284}
285
286#[derive(ToOutput, InlineOutput, Tagged, Parse, ParseInline)]
287pub struct RawPoint<T, Extra = ()> {
288 inner: RawPointInner,
289 extra: Extras<Extra>,
290 object: ObjectMarker<T>,
291}
292
293impl<T, Extra> ListHashes for RawPoint<T, Extra> {
294 fn list_hashes(&self, f: &mut impl FnMut(Hash)) {
295 self.inner.list_hashes(f);
296 }
297
298 fn topology_hash(&self) -> Hash {
299 self.inner.topology_hash()
300 }
301
302 fn point_count(&self) -> usize {
303 self.inner.point_count()
304 }
305}
306
307impl<T, Extra: 'static + Clone> FromInner for RawPoint<T, Extra> {
308 type Inner = RawPointInner;
309 type Extra = Extra;
310
311 fn from_inner(inner: Self::Inner, extra: Self::Extra) -> Self {
312 RawPoint {
313 inner,
314 extra: Extras(extra),
315 object: Default::default(),
316 }
317 }
318}
319
320impl<T, Extra: Clone> Clone for RawPoint<T, Extra> {
321 fn clone(&self) -> Self {
322 Self {
323 inner: self.inner.clone(),
324 extra: self.extra.clone(),
325 object: Default::default(),
326 }
327 }
328}
329
330impl<T: 'static + Traversible, Extra: 'static + Send + Sync + Clone + ExtraFor<T>> Topological
331 for RawPoint<T, Extra>
332{
333 fn traverse(&self, visitor: &mut impl PointVisitor) {
334 visitor.visit(self);
335 }
336}
337
338impl<T, Extra: Send + Sync> Singular for RawPoint<T, Extra> {
339 fn hash(&self) -> Hash {
340 self.inner.hash()
341 }
342}
343
344impl<T, Extra: 'static + Clone> RawPoint<T, Extra> {
345 pub fn cast<U>(self) -> RawPoint<U, Extra> {
346 self.inner.cast(self.extra.0)
347 }
348}
349
350impl<T: 'static + FullHash, Extra: 'static + Send + Sync + ExtraFor<T>> RawPoint<T, Extra> {
351 pub fn into_point(self) -> Point<T> {
352 Point::from_fetch(self.inner.hash, self.into_dyn_fetch())
353 }
354}
355
356impl<T, Extra> FetchBytes for RawPoint<T, Extra> {
357 fn fetch_bytes(&'_ self) -> FailFuture<'_, ByteNode> {
358 self.inner.fetch_bytes()
359 }
360
361 fn fetch_data(&'_ self) -> FailFuture<'_, Vec<u8>> {
362 self.inner.fetch_data()
363 }
364
365 fn fetch_bytes_local(&self) -> object_rainbow::Result<Option<ByteNode>> {
366 self.inner.fetch_bytes_local()
367 }
368
369 fn fetch_data_local(&self) -> Option<Vec<u8>> {
370 self.inner.fetch_data_local()
371 }
372
373 fn as_inner(&self) -> Option<&dyn Any> {
374 Some(&self.inner)
375 }
376
377 fn as_resolve(&self) -> Option<&Arc<dyn Resolve>> {
378 self.inner.as_resolve()
379 }
380
381 fn try_unwrap_resolve(self: Arc<Self>) -> Option<Arc<dyn Resolve>> {
382 Arc::try_unwrap(self).ok()?.inner.fetch.try_unwrap_resolve()
383 }
384}
385
386impl<T: FullHash, Extra: Send + Sync + ExtraFor<T>> Fetch for RawPoint<T, Extra> {
387 type T = T;
388
389 fn fetch_full(&'_ self) -> FailFuture<'_, Node<Self::T>> {
390 Box::pin(async {
391 let (data, resolve) = self.inner.fetch.fetch_bytes().await?;
392 let object = self
393 .extra
394 .0
395 .parse_checked(self.inner.hash, &data, &resolve)?;
396 Ok((object, resolve))
397 })
398 }
399
400 fn fetch(&'_ self) -> FailFuture<'_, Self::T> {
401 Box::pin(async {
402 let (data, resolve) = self.inner.fetch.fetch_bytes().await?;
403 self.extra.0.parse_checked(self.inner.hash, &data, &resolve)
404 })
405 }
406
407 fn try_fetch_local(&self) -> object_rainbow::Result<Option<Node<Self::T>>> {
408 let Some((data, resolve)) = self.inner.fetch.fetch_bytes_local()? else {
409 return Ok(None);
410 };
411 let object = self
412 .extra
413 .0
414 .parse_checked(self.inner.hash, &data, &resolve)?;
415 Ok(Some((object, resolve)))
416 }
417}
418
419impl<T> Point<T> {
420 pub fn from_fetch(hash: Hash, fetch: Arc<dyn Fetch<T = T>>) -> Self {
421 Self {
422 hash: hash.into(),
423 fetch,
424 }
425 }
426
427 fn map_fetch<U>(
428 self,
429 f: impl FnOnce(Arc<dyn Fetch<T = T>>) -> Arc<dyn Fetch<T = U>>,
430 ) -> Point<U> {
431 Point {
432 hash: self.hash,
433 fetch: f(self.fetch),
434 }
435 }
436}
437
438impl<U: 'static + Equivalent<T>, T: 'static, Extra> Equivalent<RawPoint<T, Extra>>
439 for RawPoint<U, Extra>
440{
441 fn into_equivalent(self) -> RawPoint<T, Extra> {
442 RawPoint {
443 inner: self.inner,
444 extra: self.extra,
445 object: Default::default(),
446 }
447 }
448
449 fn from_equivalent(object: RawPoint<T, Extra>) -> Self {
450 Self {
451 inner: object.inner,
452 extra: object.extra,
453 object: Default::default(),
454 }
455 }
456}
457
458#[derive(ParseAsInline)]
459#[must_use]
460pub struct Point<T> {
461 hash: OptionalHash,
462 fetch: Arc<dyn Fetch<T = T>>,
463}
464
465impl<T> std::fmt::Debug for Point<T> {
466 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
467 #[derive(Debug)]
468 struct Arc;
469 f.debug_struct("Point")
470 .field("hash", &self.hash)
471 .field("fetch", &Arc)
472 .finish()
473 }
474}
475
476impl<T> Point<T> {
477 pub fn raw<Extra: 'static + Clone>(self, extra: Extra) -> RawPoint<T, Extra> {
478 {
479 if let Some(raw) = self.fetch.inner_cast(&extra) {
480 return raw;
481 }
482 }
483 RawPointInner {
484 hash: self.hash.unwrap(),
485 fetch: self.fetch,
486 }
487 .cast(extra)
488 }
489}
490
491impl<T> PartialOrd for Point<T> {
492 fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
493 Some(self.cmp(other))
494 }
495}
496
497impl<T> Ord for Point<T> {
498 fn cmp(&self, other: &Self) -> std::cmp::Ordering {
499 self.hash().cmp(&other.hash())
500 }
501}
502
503impl<T> Eq for Point<T> {}
504
505impl<T> PartialEq for Point<T> {
506 fn eq(&self, other: &Self) -> bool {
507 self.hash() == other.hash()
508 }
509}
510
511impl<T> Clone for Point<T> {
512 fn clone(&self) -> Self {
513 Self {
514 hash: self.hash,
515 fetch: self.fetch.clone(),
516 }
517 }
518}
519
520impl<T> Size for Point<T> {
521 const SIZE: usize = Hash::SIZE;
522 type Size = <Hash as Size>::Size;
523}
524
525impl<T: 'static + FullHash> Point<T>
526where
527 (): ExtraFor<T>,
528{
529 pub fn from_address(address: Address, resolve: Arc<dyn Resolve>) -> Self {
530 Self::from_address_extra(address, resolve, ())
531 }
532}
533
534impl<T: 'static + FullHash> Point<T> {
535 pub fn from_address_extra<Extra: 'static + Send + Sync + Clone + ExtraFor<T>>(
536 address: Address,
537 resolve: Arc<dyn Resolve>,
538 extra: Extra,
539 ) -> Self {
540 Self::from_fetch(
541 address.hash,
542 ByAddress::from_inner(ByAddressInner { address, resolve }, extra).into_dyn_fetch(),
543 )
544 }
545
546 pub fn with_resolve<Extra: 'static + Send + Sync + Clone + ExtraFor<T>>(
547 &self,
548 resolve: Arc<dyn Resolve>,
549 extra: Extra,
550 ) -> Self {
551 Self::from_address_extra(Address::from_hash(self.hash()), resolve, extra)
552 }
553}
554
555impl<T> ListHashes for Point<T> {
556 fn list_hashes(&self, f: &mut impl FnMut(Hash)) {
557 f(self.hash());
558 }
559
560 fn point_count(&self) -> usize {
561 1
562 }
563}
564
565impl<T: Traversible> Topological for Point<T> {
566 fn traverse(&self, visitor: &mut impl PointVisitor) {
567 visitor.visit(self);
568 }
569}
570
571impl<T: 'static + FullHash, I: PointInput<Extra: Send + Sync + ExtraFor<T>>> ParseInline<I>
572 for Point<T>
573{
574 fn parse_inline(input: &mut I) -> object_rainbow::Result<Self> {
575 Ok(Self::from_address_extra(
576 input.parse_inline()?,
577 input.resolve(),
578 input.extra().clone(),
579 ))
580 }
581}
582
583impl<T: Tagged> Tagged for Point<T> {
584 const TAGS: Tags = T::TAGS;
585}
586
587impl<T> ToOutput for Point<T> {
588 fn to_output(&self, output: &mut dyn Output) {
589 self.hash().to_output(output);
590 }
591}
592
593impl<T> InlineOutput for Point<T> {}
594
595impl<T> FetchBytes for Point<T> {
596 fn fetch_bytes(&'_ self) -> FailFuture<'_, ByteNode> {
597 self.fetch.fetch_bytes()
598 }
599
600 fn fetch_data(&'_ self) -> FailFuture<'_, Vec<u8>> {
601 self.fetch.fetch_data()
602 }
603
604 fn fetch_bytes_local(&self) -> object_rainbow::Result<Option<ByteNode>> {
605 self.fetch.fetch_bytes_local()
606 }
607
608 fn fetch_data_local(&self) -> Option<Vec<u8>> {
609 self.fetch.fetch_data_local()
610 }
611
612 fn as_inner(&self) -> Option<&dyn Any> {
613 self.fetch.as_inner()
614 }
615
616 fn as_resolve(&self) -> Option<&Arc<dyn Resolve>> {
617 self.fetch.as_resolve()
618 }
619
620 fn try_unwrap_resolve(self: Arc<Self>) -> Option<Arc<dyn Resolve>> {
621 Arc::try_unwrap(self).ok()?.fetch.try_unwrap_resolve()
622 }
623}
624
625impl<T> Singular for Point<T> {
626 fn hash(&self) -> Hash {
627 self.hash.unwrap()
628 }
629}
630
631impl<T> Point<T> {
632 pub fn get(&self) -> Option<&T> {
633 self.fetch.get()
634 }
635
636 pub fn try_fetch_local(&self) -> object_rainbow::Result<Option<Node<T>>> {
637 self.fetch.try_fetch_local()
638 }
639
640 pub fn try_unwrap(self) -> Option<T> {
641 self.fetch.try_unwrap()
642 }
643}
644
645impl<T: Traversible + Clone> Point<T> {
646 pub fn from_object(object: T) -> Self {
647 Self::from_fetch(object.full_hash(), LocalFetch { object }.into_dyn_fetch())
648 }
649
650 fn yolo_mut(&mut self) -> bool {
651 self.fetch.get().is_some()
652 && Arc::get_mut(&mut self.fetch).is_some_and(|fetch| fetch.get_mut().is_some())
653 }
654
655 async fn prepare_yolo_fetch(&mut self) -> object_rainbow::Result<()> {
656 if !self.yolo_mut() {
657 let object = self.fetch.fetch().await?;
658 self.fetch = LocalFetch { object }.into_dyn_fetch();
659 }
660 Ok(())
661 }
662
663 pub async fn fetch_mut(&'_ mut self) -> object_rainbow::Result<PointMut<'_, T>> {
664 self.prepare_yolo_fetch().await?;
665 let fetch = Arc::get_mut(&mut self.fetch).unwrap();
666 assert!(fetch.get_mut().is_some());
667 self.hash.clear();
668 Ok(PointMut {
669 hash: &mut self.hash,
670 fetch,
671 })
672 }
673
674 pub async fn fetch_ref(&mut self) -> object_rainbow::Result<&T> {
675 self.prepare_yolo_fetch().await?;
676 Ok(self.fetch.get().unwrap())
677 }
678}
679
680impl<T: FullHash> Fetch for Point<T> {
681 type T = T;
682
683 fn fetch_full(&'_ self) -> FailFuture<'_, Node<Self::T>> {
684 self.fetch.fetch_full()
685 }
686
687 fn fetch(&'_ self) -> FailFuture<'_, Self::T> {
688 self.fetch.fetch()
689 }
690
691 fn try_fetch_local(&self) -> object_rainbow::Result<Option<Node<Self::T>>> {
692 self.fetch.try_fetch_local()
693 }
694
695 fn fetch_local(&self) -> Option<Self::T> {
696 self.fetch.fetch_local()
697 }
698
699 fn get(&self) -> Option<&Self::T> {
700 self.fetch.get()
701 }
702
703 fn get_mut(&mut self) -> Option<&mut Self::T> {
704 self.hash.clear();
705 Arc::get_mut(&mut self.fetch)?.get_mut()
706 }
707
708 fn get_mut_finalize(&mut self) {
709 let fetch = Arc::get_mut(&mut self.fetch).unwrap();
710 fetch.get_mut_finalize();
711 self.hash = fetch.get().unwrap().full_hash().into();
712 }
713
714 fn try_unwrap(self: Arc<Self>) -> Option<Self::T> {
715 Arc::try_unwrap(self).ok()?.fetch.try_unwrap()
716 }
717
718 fn into_dyn_fetch<'a>(self) -> Arc<dyn 'a + Fetch<T = Self::T>>
719 where
720 Self: 'a + Sized,
721 {
722 self.fetch
723 }
724}
725
726impl<U: 'static + Equivalent<T>, T: 'static> Equivalent<Point<T>> for Point<U> {
729 fn into_equivalent(self) -> Point<T> {
730 self.map_fetch(|fetch| {
731 MapEquivalent {
732 fetch,
733 map: U::into_equivalent,
734 }
735 .into_dyn_fetch()
736 })
737 }
738
739 fn from_equivalent(point: Point<T>) -> Self {
740 point.map_fetch(|fetch| {
741 MapEquivalent {
742 fetch,
743 map: U::from_equivalent,
744 }
745 .into_dyn_fetch()
746 })
747 }
748}
749
750impl<T> MaybeHasNiche for Point<T> {
751 type MnArray = <Hash as MaybeHasNiche>::MnArray;
752}
753
754impl<T: DefaultHash> Point<T> {
755 pub fn is_default(&self) -> bool {
756 self.hash() == T::default_hash()
757 }
758}
759
760impl<T: Default + Traversible + Clone> Default for Point<T> {
761 fn default() -> Self {
762 T::default().point()
763 }
764}
765
766pub trait IntoPoint: Traversible {
767 fn point(self) -> Point<Self>
768 where
769 Self: Clone,
770 {
771 Point::from_object(self)
772 }
773}
774
775impl<T: Traversible> IntoPoint for T {}
776
777struct LocalFetch<T> {
778 object: T,
779}
780
781impl<T: Traversible + Clone> Fetch for LocalFetch<T> {
782 type T = T;
783
784 fn fetch_full(&'_ self) -> FailFuture<'_, Node<Self::T>> {
785 Box::pin(ready(Ok((self.object.clone(), self.object.to_resolve()))))
786 }
787
788 fn fetch(&'_ self) -> FailFuture<'_, Self::T> {
789 Box::pin(ready(Ok(self.object.clone())))
790 }
791
792 fn try_fetch_local(&self) -> object_rainbow::Result<Option<Node<Self::T>>> {
793 Ok(Some((self.object.clone(), self.object.to_resolve())))
794 }
795
796 fn fetch_local(&self) -> Option<Self::T> {
797 Some(self.object.clone())
798 }
799
800 fn get(&self) -> Option<&Self::T> {
801 Some(&self.object)
802 }
803
804 fn get_mut(&mut self) -> Option<&mut Self::T> {
805 Some(&mut self.object)
806 }
807
808 fn try_unwrap(self: Arc<Self>) -> Option<Self::T> {
809 Arc::try_unwrap(self).ok().map(|Self { object }| object)
810 }
811}
812
813impl<T: Traversible> FetchBytes for LocalFetch<T> {
814 fn fetch_bytes(&'_ self) -> FailFuture<'_, ByteNode> {
815 Box::pin(ready(Ok((self.object.output(), self.object.to_resolve()))))
816 }
817
818 fn fetch_data(&'_ self) -> FailFuture<'_, Vec<u8>> {
819 Box::pin(ready(Ok(self.object.output())))
820 }
821
822 fn fetch_bytes_local(&self) -> object_rainbow::Result<Option<ByteNode>> {
823 Ok(Some((self.object.output(), self.object.to_resolve())))
824 }
825
826 fn fetch_data_local(&self) -> Option<Vec<u8>> {
827 Some(self.object.output())
828 }
829}
830
831impl<T: Traversible + Clone> Singular for LocalFetch<T> {
832 fn hash(&self) -> Hash {
833 self.object.full_hash()
834 }
835}
836
837struct MapEquivalent<T, F> {
838 fetch: Arc<dyn Fetch<T = T>>,
839 map: F,
840}
841
842impl<T, F> FetchBytes for MapEquivalent<T, F> {
843 fn fetch_bytes(&'_ self) -> FailFuture<'_, ByteNode> {
844 self.fetch.fetch_bytes()
845 }
846
847 fn fetch_data(&'_ self) -> FailFuture<'_, Vec<u8>> {
848 self.fetch.fetch_data()
849 }
850
851 fn fetch_bytes_local(&self) -> object_rainbow::Result<Option<ByteNode>> {
852 self.fetch.fetch_bytes_local()
853 }
854
855 fn fetch_data_local(&self) -> Option<Vec<u8>> {
856 self.fetch.fetch_data_local()
857 }
858
859 fn as_resolve(&self) -> Option<&Arc<dyn Resolve>> {
860 self.fetch.as_resolve()
861 }
862
863 fn try_unwrap_resolve(self: Arc<Self>) -> Option<Arc<dyn Resolve>> {
864 Arc::try_unwrap(self).ok()?.fetch.try_unwrap_resolve()
865 }
866}
867
868trait Map1<T>: Fn(T) -> Self::U {
869 type U;
870}
871
872impl<T, U, F: Fn(T) -> U> Map1<T> for F {
873 type U = U;
874}
875
876impl<T, F: Send + Sync + Map1<T>> Fetch for MapEquivalent<T, F> {
877 type T = F::U;
878
879 fn fetch_full(&'_ self) -> FailFuture<'_, Node<Self::T>> {
880 Box::pin(self.fetch.fetch_full().map_ok(|(x, r)| ((self.map)(x), r)))
881 }
882
883 fn fetch(&'_ self) -> FailFuture<'_, Self::T> {
884 Box::pin(self.fetch.fetch().map_ok(&self.map))
885 }
886
887 fn try_fetch_local(&self) -> object_rainbow::Result<Option<Node<Self::T>>> {
888 let Some((object, resolve)) = self.fetch.try_fetch_local()? else {
889 return Ok(None);
890 };
891 let object = (self.map)(object);
892 Ok(Some((object, resolve)))
893 }
894
895 fn fetch_local(&self) -> Option<Self::T> {
896 self.fetch.fetch_local().map(&self.map)
897 }
898
899 fn try_unwrap(self: Arc<Self>) -> Option<Self::T> {
900 let Self { fetch, map } = Arc::try_unwrap(self).ok()?;
901 fetch.try_unwrap().map(map)
902 }
903}
904
905pub struct PointMut<'a, T: FullHash> {
906 hash: &'a mut OptionalHash,
907 fetch: &'a mut dyn Fetch<T = T>,
908}
909
910impl<T: FullHash> Deref for PointMut<'_, T> {
911 type Target = T;
912
913 fn deref(&self) -> &Self::Target {
914 self.fetch.get().unwrap()
915 }
916}
917
918impl<T: FullHash> DerefMut for PointMut<'_, T> {
919 fn deref_mut(&mut self) -> &mut Self::Target {
920 self.fetch.get_mut().unwrap()
921 }
922}
923
924impl<T: FullHash> Drop for PointMut<'_, T> {
925 fn drop(&mut self) {
926 if !std::thread::panicking() {
927 self.finalize();
928 }
929 }
930}
931
932impl<'a, T: FullHash> PointMut<'a, T> {
933 fn finalize(&mut self) {
934 self.fetch.get_mut_finalize();
935 *self.hash = self.full_hash().into();
936 }
937}