rustica 0.12.0

Rustica is a functional programming library for the Rust language.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
//! # Isomorphism
//!
//! This module provides the `Iso` trait which represents isomorphisms between types.
//! An isomorphism is a pair of functions that convert between two types while preserving
//! their structure, with the property that converting from A to B and back to A gives
//! you the original value (and similarly for B to A and back to B).
//!
//! ## Examples
//!
//! ```rust
//! use rustica::traits::iso::Iso;
//!
//! // An isomorphism between String and Vec<char>
//! struct StringVecIso;
//!
//! impl Iso<String, Vec<char>> for StringVecIso {
//!     type From = String;
//!     type To = Vec<char>;
//!
//!     fn forward(&self, from: &Self::From) -> Self::To {
//!         from.chars().collect()
//!     }
//!
//!     fn backward(&self, to: &Self::To) -> Self::From {
//!         to.iter().collect()
//!     }
//! }
//!
//! // Using the isomorphism
//! let s = String::from("hello");
//! let vec = StringVecIso.forward(&s);
//! assert_eq!(vec, vec!['h', 'e', 'l', 'l', 'o']);
//! let s2 = StringVecIso.backward(&vec);
//! assert_eq!(s, s2);
//! ```
//!
//! ## Laws
//!
//! A valid isomorphism must satisfy these laws:
//!
//! 1. **Round-trip from A to B to A**: `backward(forward(a)) == a`
//! 2. **Round-trip from B to A to B**: `forward(backward(b)) == b`
//!
//! ## Common Use Cases
//!
//! Isomorphisms are useful for:
//!
//! 1. **Data Conversion** - When you need to seamlessly convert between equivalent representations
//! 2. **Lens and Optics** - Building blocks for lenses, prisms, and other optics
//! 3. **Domain Modeling** - Creating type-safe abstractions that map between domain concepts

use crate::prelude::{Either, Validated};
use std::marker::PhantomData;

/// A trait representing an isomorphism between two types.
///
/// An isomorphism defines a bidirectional mapping between types where converting
/// from one type to the other and back yields the original value. This preserves
/// all information during conversion.
///
/// # Type Parameters
///
/// * `A`: The first type in the isomorphism
/// * `B`: The second type in the isomorphism
///
/// Note: this trait also defines associated types `From` and `To` which are the types actually
/// used by `forward` and `backward`. Implementations in this crate typically set `From = A` and
/// `To = B`.
///
/// # Examples
///
/// Creating an isomorphism between a newtype and its inner value:
///
/// ```rust
/// use rustica::traits::iso::Iso;
///
/// // A newtype wrapper
/// struct UserId(u64);
///
/// // Isomorphism between UserId and u64
/// struct UserIdIso;
///
/// impl Iso<UserId, u64> for UserIdIso {
///     type From = UserId;
///     type To = u64;
///
///     fn forward(&self, from: &Self::From) -> Self::To {
///         from.0
///     }
///
///     fn backward(&self, to: &Self::To) -> Self::From {
///         UserId(*to)
///     }
/// }
///
/// let iso = UserIdIso;
/// let user_id = UserId(123);
/// let id_num = iso.forward(&user_id);
/// assert_eq!(id_num, 123);
///
/// let user_id2 = iso.backward(&id_num);
/// assert_eq!(user_id2.0, user_id.0);
/// ```
pub trait Iso<A, B> {
    /// The source type of the isomorphism.
    type From;

    /// The target type of the isomorphism.
    type To;

    /// Converts from the source type to the target type.
    ///
    /// # Arguments
    ///
    /// * `from` - A reference to a value of the source type
    ///
    /// # Returns
    ///
    /// A value of the target type
    ///
    /// # Examples
    ///
    /// ```rust
    /// use rustica::traits::iso::Iso;
    ///
    /// struct StringVecIso;
    ///
    /// impl Iso<String, Vec<char>> for StringVecIso {
    ///     type From = String;
    ///     type To = Vec<char>;
    ///
    ///     fn forward(&self, from: &Self::From) -> Self::To {
    ///         from.chars().collect()
    ///     }
    ///
    ///     fn backward(&self, to: &Self::To) -> Self::From {
    ///         to.iter().collect()
    ///     }
    /// }
    ///
    /// let text = String::from("hello");
    /// let chars = StringVecIso.forward(&text);
    /// assert_eq!(chars, vec!['h', 'e', 'l', 'l', 'o']);
    /// ```
    fn forward(&self, from: &Self::From) -> Self::To;

    /// Converts from the target type back to the source type.
    ///
    /// # Arguments
    ///
    /// * `to` - A reference to a value of the target type
    ///
    /// # Returns
    ///
    /// A value of the source type
    ///
    /// # Examples
    ///
    /// ```rust
    /// use rustica::traits::iso::Iso;
    ///
    /// struct StringVecIso;
    ///
    /// impl Iso<String, Vec<char>> for StringVecIso {
    ///     type From = String;
    ///     type To = Vec<char>;
    ///
    ///     fn forward(&self, from: &Self::From) -> Self::To {
    ///         from.chars().collect()
    ///     }
    ///
    ///     fn backward(&self, to: &Self::To) -> Self::From {
    ///         to.iter().collect()
    ///     }
    /// }
    ///
    /// let chars = vec!['h', 'e', 'l', 'l', 'o'];
    /// let text = StringVecIso.backward(&chars);
    /// assert_eq!(text, "hello");
    /// ```
    fn backward(&self, to: &Self::To) -> Self::From;

    /// Converts a function that operates on the target type to a function
    /// that operates on the source type.
    ///
    /// # Type Parameters
    ///
    /// * `F` - The type of the function that operates on target values
    ///
    /// # Arguments
    ///
    /// * `f` - A function that takes a reference to a target value and returns some result
    ///
    /// # Returns
    ///
    /// A function that takes a reference to a source value and returns the same result type
    ///
    /// # Examples
    ///
    /// ```rust
    /// use rustica::traits::iso::Iso;
    ///
    /// struct StringVecIso;
    ///
    /// impl Iso<String, Vec<char>> for StringVecIso {
    ///     type From = String;
    ///     type To = Vec<char>;
    ///
    ///     fn forward(&self, from: &Self::From) -> Self::To {
    ///         from.chars().collect()
    ///     }
    ///
    ///     fn backward(&self, to: &Self::To) -> Self::From {
    ///         to.iter().collect()
    ///     }
    /// }
    ///
    /// // Create a function that counts elements in the target type (Vec<char>)
    /// let count_chars = |chars: &Vec<char>| chars.len();
    ///
    /// // Convert it to operate on the source type (String)
    /// let count_string_chars = StringVecIso.map_from_target(count_chars);
    ///
    /// // Use the converted function
    /// assert_eq!(count_string_chars(&"hello".to_string()), 5);
    /// ```
    #[inline]
    fn map_from_target<F, R>(&self, f: F) -> impl Fn(&Self::From) -> R
    where
        F: Fn(&Self::To) -> R,
    {
        move |from| f(&self.forward(from))
    }

    /// Converts a function that operates on the source type to a function
    /// that operates on the target type.
    ///
    /// # Type Parameters
    ///
    /// * `F` - The type of the function that operates on source values
    ///
    /// # Arguments
    ///
    /// * `f` - A function that takes a reference to a source value and returns some result
    ///
    /// # Returns
    ///
    /// A function that takes a reference to a target value and returns the same result type
    ///
    /// # Examples
    ///
    /// ```rust
    /// use rustica::traits::iso::Iso;
    ///
    /// struct StringVecIso;
    ///
    /// impl Iso<String, Vec<char>> for StringVecIso {
    ///     type From = String;
    ///     type To = Vec<char>;
    ///
    ///     fn forward(&self, from: &Self::From) -> Self::To {
    ///         from.chars().collect()
    ///     }
    ///
    ///     fn backward(&self, to: &Self::To) -> Self::From {
    ///         to.iter().collect()
    ///     }
    /// }
    ///
    /// // Create a function that operates on the source type (String)
    /// let is_hello = |s: &String| s == "hello";
    ///
    /// // Convert it to operate on the target type (Vec<char>)
    /// let is_hello_vec = StringVecIso.map_from_source(is_hello);
    ///
    /// // Use the converted function
    /// assert!(is_hello_vec(&vec!['h', 'e', 'l', 'l', 'o']));
    /// assert!(!is_hello_vec(&vec!['w', 'o', 'r', 'l', 'd']));
    /// ```
    #[inline]
    fn map_from_source<F, R>(&self, f: F) -> impl Fn(&Self::To) -> R
    where
        F: Fn(&Self::From) -> R,
    {
        move |to| f(&self.backward(to))
    }

    /// Creates a new isomorphism by composing this isomorphism with another.
    ///
    /// Composition allows chaining conversions between multiple types.
    /// For example, if you have `A -> B` and `B -> C`, you can compose them to get `A -> C`.
    ///
    /// # Type Parameters
    ///
    /// * `C` - The target type of the second isomorphism
    /// * `ISO2` - The type of the second isomorphism
    ///
    /// # Arguments
    ///
    /// * `other` - The second isomorphism to compose with
    ///
    /// # Returns
    ///
    /// A new isomorphism that represents the composition of the two isomorphisms
    ///
    /// # Examples
    ///
    /// ```rust
    /// use rustica::traits::iso::Iso;
    ///
    /// // Isomorphism between String and Vec<char>
    /// #[derive(Clone)]
    /// struct StringVecIso;
    /// impl Iso<String, Vec<char>> for StringVecIso {
    ///     type From = String;
    ///     type To = Vec<char>;
    ///
    ///     fn forward(&self, from: &Self::From) -> Self::To {
    ///         from.chars().collect()
    ///     }
    ///
    ///     fn backward(&self, to: &Self::To) -> Self::From {
    ///         to.iter().collect()
    ///     }
    /// }
    ///
    /// // Isomorphism between Vec<char> and String length
    /// #[derive(Clone)]
    /// struct VecLenIso;
    /// impl Iso<Vec<char>, usize> for VecLenIso {
    ///     type From = Vec<char>;
    ///     type To = usize;
    ///
    ///     fn forward(&self, from: &Self::From) -> Self::To {
    ///         from.len()
    ///     }
    ///
    ///     fn backward(&self, to: &Self::To) -> Self::From {
    ///         vec!['x'; *to]
    ///     }
    /// }
    ///
    /// // Compose the two isomorphisms
    /// let string_to_len = StringVecIso.iso_compose(VecLenIso);
    ///
    /// let s = String::from("hello");
    /// assert_eq!(string_to_len.forward(&s), 5);
    ///
    /// let len = 3;
    /// assert_eq!(string_to_len.backward(&len), "xxx");
    /// ```
    fn iso_compose<C, ISO2>(&self, other: ISO2) -> ComposedIso<Self, ISO2, A, B, C>
    where
        Self: Iso<A, B> + Sized + Clone,
        Self::From: Clone,
        Self::To: Clone,
        B: Clone,
        ISO2: Iso<B, C, From = B, To = C>,
        ISO2::From: Clone,
        ISO2::To: Clone,
        C: Clone,
    {
        ComposedIso {
            first: self.clone(),
            second: other,
            _phantom: PhantomData,
        }
    }

    /// Creates an inverse isomorphism that swaps the source and target types.
    ///
    /// # Type Parameters
    ///
    /// * `A` - The source type of the original isomorphism
    /// * `B` - The target type of the original isomorphism
    ///
    /// # Returns
    ///
    /// A new isomorphism with the same types but with source and target swapped
    ///
    /// # Examples
    ///
    /// ```rust
    /// use rustica::traits::iso::Iso;
    ///
    /// // A newtype wrapper
    /// #[derive(Clone)]
    /// struct UserId(u64);
    ///
    /// // Isomorphism between UserId and u64
    /// #[derive(Clone)]
    /// struct UserIdIso;
    ///
    /// impl Iso<UserId, u64> for UserIdIso {
    ///     type From = UserId;
    ///     type To = u64;
    ///
    ///     fn forward(&self, from: &Self::From) -> Self::To {
    ///         from.0
    ///     }
    ///
    ///     fn backward(&self, to: &Self::To) -> Self::From {
    ///         UserId(*to)
    ///     }
    /// }
    ///
    /// let iso = UserIdIso;
    /// let inverse = iso.inverse();
    ///
    /// let num = 42u64;
    /// let user_id = inverse.forward(&num);  // Converts u64 to UserId
    /// assert_eq!(user_id.0, 42);
    ///
    /// let original = inverse.backward(&user_id);  // Converts UserId back to u64
    /// assert_eq!(original, 42);
    /// ```
    fn inverse(&self) -> InverseIso<Self, A, B>
    where
        Self: Sized + Clone,
    {
        InverseIso {
            original: self.clone(),
            _phantom: PhantomData,
        }
    }
}

/// An isomorphism created by composing two other isomorphisms.
///
/// This type allows chaining two isomorphisms together to create a new isomorphism
/// that transforms from type `A` to type `C` via an intermediate type `B`.
///
/// # Type Parameters
///
/// * `ISO1`: An isomorphism from `A` to `B`
/// * `ISO2`: An isomorphism from `B` to `C`
/// * `A`: The source type of the composed isomorphism
/// * `B`: The intermediate type
/// * `C`: The target type of the composed isomorphism
///
/// # Examples
///
/// ```rust
/// use rustica::traits::iso::Iso;
///
/// // Define isomorphisms for string <-> chars and chars <-> bytes
/// #[derive(Clone)]
/// struct StringCharsIso;
/// #[derive(Clone)]
/// struct CharsBytesIso;
///
/// impl Iso<String, Vec<char>> for StringCharsIso {
///     type From = String;
///     type To = Vec<char>;
///
///     fn forward(&self, from: &String) -> Vec<char> {
///         from.chars().collect()
///     }
///
///     fn backward(&self, to: &Vec<char>) -> String {
///         to.iter().collect()
///     }
/// }
///
/// impl Iso<Vec<char>, Vec<u8>> for CharsBytesIso {
///     type From = Vec<char>;
///     type To = Vec<u8>;
///
///     fn forward(&self, from: &Vec<char>) -> Vec<u8> {
///         from.iter().map(|&c| c as u8).collect()
///     }
///
///     fn backward(&self, to: &Vec<u8>) -> Vec<char> {
///         to.iter().map(|&b| b as char).collect()
///     }
/// }
///
/// // Compose the isomorphisms
/// let string_iso = StringCharsIso;
/// let bytes_iso = CharsBytesIso;
/// let composed = string_iso.iso_compose(bytes_iso);
///
/// // Use the composed isomorphism
/// let s = "Hello".to_string();
/// let bytes = composed.forward(&s);
/// let original = composed.backward(&bytes);
/// assert_eq!(original, s);
/// ```
pub struct ComposedIso<ISO1, ISO2, A, B, C>
where
    ISO1: Iso<A, B>,
    ISO2: Iso<B, C>,
{
    pub first: ISO1,
    pub second: ISO2,
    pub _phantom: PhantomData<(A, B, C)>,
}

impl<ISO1, ISO2, A, B, C> Iso<A, C> for ComposedIso<ISO1, ISO2, A, B, C>
where
    ISO1: Iso<A, B, From = A, To = B>,
    ISO2: Iso<B, C, From = B, To = C>,
    A: Clone,
    B: Clone,
    C: Clone,
{
    type From = A;
    type To = C;

    fn forward(&self, from: &Self::From) -> Self::To {
        // Since we've constrained the types to be equal, we can use them directly
        let b = self.first.forward(from);
        self.second.forward(&b)
    }

    fn backward(&self, to: &Self::To) -> Self::From {
        // Since we've constrained the types to be equal, we can use them directly
        let b = self.second.backward(to);
        self.first.backward(&b)
    }
}

/// An isomorphism that inverts the direction of another isomorphism.
///
/// This struct allows you to flip the direction of an existing isomorphism,
/// effectively swapping the `forward` and `backward` operations.
///
/// # Type Parameters
///
/// * `ISO` - The original isomorphism type
/// * `A` - The source type of the original isomorphism
/// * `B` - The target type of the original isomorphism
///
/// # Examples
///
/// ```rust
/// use rustica::traits::iso::Iso;
/// use std::marker::PhantomData;
///
/// // Define an isomorphism between String and Vec<char>
/// #[derive(Clone)]
/// struct StringVecIso;
///
/// impl Iso<String, Vec<char>> for StringVecIso {
///     type From = String;
///     type To = Vec<char>;
///
///     fn forward(&self, from: &Self::From) -> Self::To {
///         from.chars().collect()
///     }
///
///     fn backward(&self, to: &Self::To) -> Self::From {
///         to.iter().collect()
///     }
/// }
///
/// // Create and use the inverse isomorphism
/// let iso = StringVecIso;
/// let inverse = iso.inverse();
///
/// let chars = vec!['h', 'e', 'l', 'l', 'o'];
/// let string = inverse.forward(&chars);
/// assert_eq!(string, "hello");
///
/// let chars2 = inverse.backward(&string);
/// assert_eq!(chars, chars2);
/// ```
pub struct InverseIso<ISO, A, B>
where
    ISO: Iso<A, B>,
{
    original: ISO,
    _phantom: PhantomData<(A, B)>,
}

impl<ISO, A, B> Iso<B, A> for InverseIso<ISO, A, B>
where
    ISO: Iso<A, B, From = A, To = B>,
    A: Clone,
    B: Clone,
{
    type From = B;
    type To = A;

    fn forward(&self, from: &Self::From) -> Self::To {
        // Since we've constrained the types to be equal, we can use them directly
        self.original.backward(from)
    }

    fn backward(&self, to: &Self::To) -> Self::From {
        // Since we've constrained the types to be equal, we can use them directly
        self.original.forward(to)
    }
}

/// Extension methods for types that implement `Iso`.
pub trait IsoExt<A, B>: Iso<A, B> {
    /// Applies this isomorphism to convert a value of the source type into the target type.
    ///
    /// # Arguments
    ///
    /// * `value` - A value of the source type
    ///
    /// # Returns
    ///
    /// The converted value in the target type
    fn convert_forward(&self, value: &Self::From) -> Self::To {
        self.forward(value)
    }

    /// Applies this isomorphism to convert a value of the target type back to the source type.
    ///
    /// # Arguments
    ///
    /// * `value` - A value of the target type
    ///
    /// # Returns
    ///
    /// The converted value in the source type
    fn convert_backward(&self, value: &Self::To) -> Self::From {
        self.backward(value)
    }

    /// Modifies a value of the source type by applying a function to its representation
    /// in the target type.
    ///
    /// # Type Parameters
    ///
    /// * `F` - The type of the function that modifies target values
    ///
    /// # Arguments
    ///
    /// * `from` - A value of the source type
    /// * `f` - A function that transforms a target value into another target value
    ///
    /// # Returns
    ///
    /// The modified value in the source type
    fn modify<F>(&self, from: &Self::From, f: F) -> Self::From
    where
        F: FnOnce(Self::To) -> Self::To,
    {
        self.backward(&f(self.forward(from)))
    }

    /// Verifies that this isomorphism satisfies the isomorphism laws for the given values.
    ///
    /// # Arguments
    ///
    /// * `from` - A value of the source type
    /// * `to` - A value of the target type
    ///
    /// # Returns
    ///
    /// `true` if the isomorphism laws are satisfied for the given values,
    /// `false` otherwise
    fn verify_laws(&self, from: &Self::From, to: &Self::To) -> bool
    where
        Self::From: PartialEq,
        Self::To: PartialEq,
    {
        let round_trip_from = self.backward(&self.forward(from));
        let round_trip_to = self.forward(&self.backward(to));

        &round_trip_from == from && &round_trip_to == to
    }
}

// Implement IsoExt for all types that implement Iso
impl<T, A, B> IsoExt<A, B> for T where T: Iso<A, B> {}

/// An isomorphism between Result<R, L> and Either<L, R>.
///
/// # Example
/// ```rust
/// use rustica::traits::iso::{Iso, ResultEitherIso};
/// use rustica::prelude::Either;
/// let iso = ResultEitherIso;
/// let res: Result<i32, &str> = Ok(7);
/// let either = iso.forward(&res);
/// assert_eq!(either, Either::right(7));
/// let res2 = iso.backward(&either);
/// assert_eq!(res2, Ok(7));
/// let err: Result<i32, &str> = Err("fail");
/// let either2 = iso.forward(&err);
/// assert_eq!(either2, Either::left("fail"));
/// let res3 = iso.backward(&either2);
/// assert_eq!(res3, Err("fail"));
/// ```
pub struct ResultEitherIso;

impl<R: Clone, L: Clone> Iso<Result<R, L>, Either<L, R>> for ResultEitherIso {
    type From = Result<R, L>;
    type To = Either<L, R>;

    fn forward(&self, from: &Self::From) -> Self::To {
        Either::from_result(from.clone())
    }

    fn backward(&self, to: &Self::To) -> Self::From {
        to.clone().to_result()
    }
}

/// An isomorphism between Result<A, E> and Validated<E, A>.
///
/// # Example
/// ```rust
/// use rustica::traits::iso::{Iso, ResultValidatedIso};
/// use rustica::datatypes::validated::Validated;
/// let iso = ResultValidatedIso;
/// let res: Result<i32, &str> = Ok(42);
/// let validated = iso.forward(&res);
/// assert_eq!(validated, Validated::valid(42));
/// let res2 = iso.backward(&validated);
/// assert_eq!(res2, Ok(42));
/// let err: Result<i32, &str> = Err("fail");
/// let validated2 = iso.forward(&err);
/// assert!(validated2.is_invalid());
/// let res3 = iso.backward(&validated2);
/// assert_eq!(res3, Err("fail"));
/// ```
pub struct ResultValidatedIso;

impl<A: Clone, E: Clone> Iso<Result<A, E>, Validated<E, A>> for ResultValidatedIso {
    type From = Result<A, E>;
    type To = Validated<E, A>;

    fn forward(&self, from: &Self::From) -> Self::To {
        Validated::from_result(from)
    }

    fn backward(&self, to: &Self::To) -> Self::From {
        to.to_result()
    }
}