rsdiff_core/ops/kinds/binary/
args.rs

1/*
2   Appellation: args <binary>
3   Contrib: FL03 <jo3mccain@icloud.com>
4*/
5use crate::ops::Params;
6use core::mem;
7
8pub trait BinaryParams: Params<Pattern = (Self::Lhs, Self::Rhs)> {
9    type Lhs;
10    type Rhs;
11
12    fn lhs(&self) -> &Self::Lhs;
13
14    fn rhs(&self) -> &Self::Rhs;
15}
16
17pub struct BinaryArgs<A, B = A> {
18    pub lhs: A,
19    pub rhs: B,
20}
21
22impl<A, B> BinaryArgs<A, B> {
23    pub fn new(lhs: A, rhs: B) -> Self {
24        Self { lhs, rhs }
25    }
26
27    pub fn from_params<P>(params: P) -> Self
28    where
29        P: Params<Pattern = (A, B)>,
30    {
31        let (lhs, rhs) = params.into_pattern();
32        Self::new(lhs, rhs)
33    }
34
35    pub fn into_args(self) -> (A, B) {
36        (self.lhs, self.rhs)
37    }
38
39    pub fn flip(self) -> BinaryArgs<B, A> {
40        BinaryArgs::new(self.rhs, self.lhs)
41    }
42
43    pub fn lhs(&self) -> &A {
44        &self.lhs
45    }
46
47    pub fn rhs(&self) -> &B {
48        &self.rhs
49    }
50}
51
52impl<T> BinaryArgs<T, T> {
53    pub fn swap(&mut self) {
54        mem::swap(&mut self.lhs, &mut self.rhs);
55    }
56}
57
58impl<A, B> BinaryParams for BinaryArgs<A, B> {
59    type Lhs = A;
60    type Rhs = B;
61
62    fn lhs(&self) -> &Self::Lhs {
63        self.lhs()
64    }
65
66    fn rhs(&self) -> &Self::Rhs {
67        self.rhs()
68    }
69}
70
71impl<A, B> Params for BinaryArgs<A, B> {
72    type Pattern = (A, B);
73
74    fn into_pattern(self) -> Self::Pattern {
75        (self.lhs, self.rhs)
76    }
77}
78
79impl<A, B> From<BinaryArgs<A, B>> for (A, B) {
80    fn from(args: BinaryArgs<A, B>) -> Self {
81        args.into_pattern()
82    }
83}
84
85impl<A, B> From<&BinaryArgs<A, B>> for (A, B)
86where
87    A: Clone,
88    B: Clone,
89{
90    fn from(args: &BinaryArgs<A, B>) -> Self {
91        (args.lhs.clone(), args.rhs.clone())
92    }
93}
94
95impl<A, B> BinaryParams for (A, B) {
96    type Lhs = A;
97    type Rhs = B;
98
99    fn lhs(&self) -> &Self::Lhs {
100        &self.0
101    }
102
103    fn rhs(&self) -> &Self::Rhs {
104        &self.1
105    }
106}