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, Equivalent, ExtraFor, FailFuture, Fetch, FetchBytes, FullHash, Hash,
15 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)
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)
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, Arc::new(self))
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 Arc::new(ByAddress::from_inner(
543 ByAddressInner { address, resolve },
544 extra,
545 )),
546 )
547 }
548
549 pub fn with_resolve<Extra: 'static + Send + Sync + Clone + ExtraFor<T>>(
550 &self,
551 resolve: Arc<dyn Resolve>,
552 extra: Extra,
553 ) -> Self {
554 Self::from_address_extra(Address::from_hash(self.hash()), resolve, extra)
555 }
556}
557
558impl<T> ListHashes for Point<T> {
559 fn list_hashes(&self, f: &mut impl FnMut(Hash)) {
560 f(self.hash());
561 }
562
563 fn point_count(&self) -> usize {
564 1
565 }
566}
567
568impl<T: Traversible> Topological for Point<T> {
569 fn traverse(&self, visitor: &mut impl PointVisitor) {
570 visitor.visit(self);
571 }
572}
573
574impl<T: 'static + FullHash, I: PointInput<Extra: Send + Sync + ExtraFor<T>>> ParseInline<I>
575 for Point<T>
576{
577 fn parse_inline(input: &mut I) -> object_rainbow::Result<Self> {
578 Ok(Self::from_address_extra(
579 input.parse_inline()?,
580 input.resolve(),
581 input.extra().clone(),
582 ))
583 }
584}
585
586impl<T: Tagged> Tagged for Point<T> {
587 const TAGS: Tags = T::TAGS;
588}
589
590impl<T> ToOutput for Point<T> {
591 fn to_output(&self, output: &mut dyn Output) {
592 self.hash().to_output(output);
593 }
594}
595
596impl<T> InlineOutput for Point<T> {}
597
598impl<T> FetchBytes for Point<T> {
599 fn fetch_bytes(&'_ self) -> FailFuture<'_, ByteNode> {
600 self.fetch.fetch_bytes()
601 }
602
603 fn fetch_data(&'_ self) -> FailFuture<'_, Vec<u8>> {
604 self.fetch.fetch_data()
605 }
606
607 fn fetch_bytes_local(&self) -> object_rainbow::Result<Option<ByteNode>> {
608 self.fetch.fetch_bytes_local()
609 }
610
611 fn fetch_data_local(&self) -> Option<Vec<u8>> {
612 self.fetch.fetch_data_local()
613 }
614
615 fn as_inner(&self) -> Option<&dyn Any> {
616 self.fetch.as_inner()
617 }
618
619 fn as_resolve(&self) -> Option<&Arc<dyn Resolve>> {
620 self.fetch.as_resolve()
621 }
622
623 fn try_unwrap_resolve(self: Arc<Self>) -> Option<Arc<dyn Resolve>> {
624 Arc::try_unwrap(self).ok()?.fetch.try_unwrap_resolve()
625 }
626}
627
628impl<T> Singular for Point<T> {
629 fn hash(&self) -> Hash {
630 self.hash.unwrap()
631 }
632}
633
634impl<T> Point<T> {
635 pub fn get(&self) -> Option<&T> {
636 self.fetch.get()
637 }
638
639 pub fn try_fetch_local(&self) -> object_rainbow::Result<Option<Node<T>>> {
640 self.fetch.try_fetch_local()
641 }
642
643 pub fn try_unwrap(self) -> Option<T> {
644 self.fetch.try_unwrap()
645 }
646}
647
648impl<T: Traversible + Clone> Point<T> {
649 pub fn from_object(object: T) -> Self {
650 Self::from_fetch(object.full_hash(), Arc::new(LocalFetch { object }))
651 }
652
653 fn yolo_mut(&mut self) -> bool {
654 self.fetch.get().is_some()
655 && Arc::get_mut(&mut self.fetch).is_some_and(|fetch| fetch.get_mut().is_some())
656 }
657
658 async fn prepare_yolo_fetch(&mut self) -> object_rainbow::Result<()> {
659 if !self.yolo_mut() {
660 let object = self.fetch.fetch().await?;
661 self.fetch = Arc::new(LocalFetch { object });
662 }
663 Ok(())
664 }
665
666 pub async fn fetch_mut(&'_ mut self) -> object_rainbow::Result<PointMut<'_, T>> {
667 self.prepare_yolo_fetch().await?;
668 let fetch = Arc::get_mut(&mut self.fetch).unwrap();
669 assert!(fetch.get_mut().is_some());
670 self.hash.clear();
671 Ok(PointMut {
672 hash: &mut self.hash,
673 fetch,
674 })
675 }
676
677 pub async fn fetch_ref(&mut self) -> object_rainbow::Result<&T> {
678 self.prepare_yolo_fetch().await?;
679 Ok(self.fetch.get().unwrap())
680 }
681}
682
683impl<T: FullHash> Fetch for Point<T> {
684 type T = T;
685
686 fn fetch_full(&'_ self) -> FailFuture<'_, Node<Self::T>> {
687 self.fetch.fetch_full()
688 }
689
690 fn fetch(&'_ self) -> FailFuture<'_, Self::T> {
691 self.fetch.fetch()
692 }
693
694 fn try_fetch_local(&self) -> object_rainbow::Result<Option<Node<Self::T>>> {
695 self.fetch.try_fetch_local()
696 }
697
698 fn fetch_local(&self) -> Option<Self::T> {
699 self.fetch.fetch_local()
700 }
701
702 fn get(&self) -> Option<&Self::T> {
703 self.fetch.get()
704 }
705
706 fn get_mut(&mut self) -> Option<&mut Self::T> {
707 self.hash.clear();
708 Arc::get_mut(&mut self.fetch)?.get_mut()
709 }
710
711 fn get_mut_finalize(&mut self) {
712 let fetch = Arc::get_mut(&mut self.fetch).unwrap();
713 fetch.get_mut_finalize();
714 self.hash = fetch.get().unwrap().full_hash().into();
715 }
716
717 fn try_unwrap(self: Arc<Self>) -> Option<Self::T> {
718 Arc::try_unwrap(self).ok()?.fetch.try_unwrap()
719 }
720}
721
722impl<U: 'static + Equivalent<T>, T: 'static> Equivalent<Point<T>> for Point<U> {
725 fn into_equivalent(self) -> Point<T> {
726 self.map_fetch(|fetch| {
727 Arc::new(MapEquivalent {
728 fetch,
729 map: U::into_equivalent,
730 })
731 })
732 }
733
734 fn from_equivalent(point: Point<T>) -> Self {
735 point.map_fetch(|fetch| {
736 Arc::new(MapEquivalent {
737 fetch,
738 map: U::from_equivalent,
739 })
740 })
741 }
742}
743
744impl<T> MaybeHasNiche for Point<T> {
745 type MnArray = <Hash as MaybeHasNiche>::MnArray;
746}
747
748impl<T: FullHash + Default> Point<T> {
749 pub fn is_default(&self) -> bool {
750 self.hash() == T::default().full_hash()
751 }
752}
753
754impl<T: Default + Traversible + Clone> Default for Point<T> {
755 fn default() -> Self {
756 T::default().point()
757 }
758}
759
760pub trait IntoPoint: Traversible {
761 fn point(self) -> Point<Self>
762 where
763 Self: Clone,
764 {
765 Point::from_object(self)
766 }
767}
768
769impl<T: Traversible> IntoPoint for T {}
770
771struct LocalFetch<T> {
772 object: T,
773}
774
775impl<T: Traversible + Clone> Fetch for LocalFetch<T> {
776 type T = T;
777
778 fn fetch_full(&'_ self) -> FailFuture<'_, Node<Self::T>> {
779 Box::pin(ready(Ok((self.object.clone(), self.object.to_resolve()))))
780 }
781
782 fn fetch(&'_ self) -> FailFuture<'_, Self::T> {
783 Box::pin(ready(Ok(self.object.clone())))
784 }
785
786 fn try_fetch_local(&self) -> object_rainbow::Result<Option<Node<Self::T>>> {
787 Ok(Some((self.object.clone(), self.object.to_resolve())))
788 }
789
790 fn fetch_local(&self) -> Option<Self::T> {
791 Some(self.object.clone())
792 }
793
794 fn get(&self) -> Option<&Self::T> {
795 Some(&self.object)
796 }
797
798 fn get_mut(&mut self) -> Option<&mut Self::T> {
799 Some(&mut self.object)
800 }
801
802 fn try_unwrap(self: Arc<Self>) -> Option<Self::T> {
803 Arc::try_unwrap(self).ok().map(|Self { object }| object)
804 }
805}
806
807impl<T: Traversible> FetchBytes for LocalFetch<T> {
808 fn fetch_bytes(&'_ self) -> FailFuture<'_, ByteNode> {
809 Box::pin(ready(Ok((self.object.output(), self.object.to_resolve()))))
810 }
811
812 fn fetch_data(&'_ self) -> FailFuture<'_, Vec<u8>> {
813 Box::pin(ready(Ok(self.object.output())))
814 }
815
816 fn fetch_bytes_local(&self) -> object_rainbow::Result<Option<ByteNode>> {
817 Ok(Some((self.object.output(), self.object.to_resolve())))
818 }
819
820 fn fetch_data_local(&self) -> Option<Vec<u8>> {
821 Some(self.object.output())
822 }
823}
824
825impl<T: Traversible + Clone> Singular for LocalFetch<T> {
826 fn hash(&self) -> Hash {
827 self.object.full_hash()
828 }
829}
830
831struct MapEquivalent<T, F> {
832 fetch: Arc<dyn Fetch<T = T>>,
833 map: F,
834}
835
836impl<T, F> FetchBytes for MapEquivalent<T, F> {
837 fn fetch_bytes(&'_ self) -> FailFuture<'_, ByteNode> {
838 self.fetch.fetch_bytes()
839 }
840
841 fn fetch_data(&'_ self) -> FailFuture<'_, Vec<u8>> {
842 self.fetch.fetch_data()
843 }
844
845 fn fetch_bytes_local(&self) -> object_rainbow::Result<Option<ByteNode>> {
846 self.fetch.fetch_bytes_local()
847 }
848
849 fn fetch_data_local(&self) -> Option<Vec<u8>> {
850 self.fetch.fetch_data_local()
851 }
852
853 fn as_resolve(&self) -> Option<&Arc<dyn Resolve>> {
854 self.fetch.as_resolve()
855 }
856
857 fn try_unwrap_resolve(self: Arc<Self>) -> Option<Arc<dyn Resolve>> {
858 Arc::try_unwrap(self).ok()?.fetch.try_unwrap_resolve()
859 }
860}
861
862trait Map1<T>: Fn(T) -> Self::U {
863 type U;
864}
865
866impl<T, U, F: Fn(T) -> U> Map1<T> for F {
867 type U = U;
868}
869
870impl<T, F: Send + Sync + Map1<T>> Fetch for MapEquivalent<T, F> {
871 type T = F::U;
872
873 fn fetch_full(&'_ self) -> FailFuture<'_, Node<Self::T>> {
874 Box::pin(self.fetch.fetch_full().map_ok(|(x, r)| ((self.map)(x), r)))
875 }
876
877 fn fetch(&'_ self) -> FailFuture<'_, Self::T> {
878 Box::pin(self.fetch.fetch().map_ok(&self.map))
879 }
880
881 fn try_fetch_local(&self) -> object_rainbow::Result<Option<Node<Self::T>>> {
882 let Some((object, resolve)) = self.fetch.try_fetch_local()? else {
883 return Ok(None);
884 };
885 let object = (self.map)(object);
886 Ok(Some((object, resolve)))
887 }
888
889 fn fetch_local(&self) -> Option<Self::T> {
890 self.fetch.fetch_local().map(&self.map)
891 }
892
893 fn try_unwrap(self: Arc<Self>) -> Option<Self::T> {
894 let Self { fetch, map } = Arc::try_unwrap(self).ok()?;
895 fetch.try_unwrap().map(map)
896 }
897}
898
899pub struct PointMut<'a, T: FullHash> {
900 hash: &'a mut OptionalHash,
901 fetch: &'a mut dyn Fetch<T = T>,
902}
903
904impl<T: FullHash> Deref for PointMut<'_, T> {
905 type Target = T;
906
907 fn deref(&self) -> &Self::Target {
908 self.fetch.get().unwrap()
909 }
910}
911
912impl<T: FullHash> DerefMut for PointMut<'_, T> {
913 fn deref_mut(&mut self) -> &mut Self::Target {
914 self.fetch.get_mut().unwrap()
915 }
916}
917
918impl<T: FullHash> Drop for PointMut<'_, T> {
919 fn drop(&mut self) {
920 self.finalize();
921 }
922}
923
924impl<'a, T: FullHash> PointMut<'a, T> {
925 fn finalize(&mut self) {
926 self.fetch.get_mut_finalize();
927 *self.hash = self.full_hash().into();
928 }
929}