Skip to content

Commit 8329a74

Browse files
authored
Merge branch 'master' into updated-flake-lock
2 parents fe6f16f + 4c0589f commit 8329a74

File tree

11 files changed

+510
-26
lines changed

11 files changed

+510
-26
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ true = { version = "0.1.0", optional = true }
1717
data-encoding = { version = "2.4.0", optional = true }
1818
thiserror = "1.0.50"
1919
linked-hash-map = "0.5.6"
20+
num-traits = "0.2.17"
2021

2122
[features]
2223
serde = ["dep:serde", "num-bigint/serde"]

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ pub mod v1;
77
pub mod v2;
88
#[cfg(feature = "lbf")]
99
pub use lbr_prelude::json;
10-
pub(crate) mod utils;
10+
pub mod utils;

src/utils.rs

-17
This file was deleted.

src/utils/mod.rs

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use std::{
2+
collections::BTreeMap,
3+
iter::{empty, once},
4+
};
5+
6+
pub mod value;
7+
8+
/// Create a container C from one element.
9+
pub fn singleton<T, C>(value: T) -> C
10+
where
11+
C: FromIterator<T>,
12+
{
13+
once(value).collect()
14+
}
15+
16+
/// Create an empty container.
17+
pub fn none<T, C>() -> C
18+
where
19+
C: FromIterator<T>,
20+
{
21+
empty::<T>().collect()
22+
}
23+
24+
/// Union two BTreeMaps, call f to resolve conflicts if duplicate keys are encountered.
25+
pub fn union_btree_maps_with<K: Clone + Ord, V: Clone, F: Fn(V, V) -> V>(
26+
f: F,
27+
l: BTreeMap<K, V>,
28+
r: BTreeMap<K, V>,
29+
) -> BTreeMap<K, V> {
30+
r.into_iter().fold(l.clone(), |mut acc, (k, vr)| {
31+
let v = if let Some((_, vl)) = acc.remove_entry(&k) {
32+
f(vl, vr)
33+
} else {
34+
vr
35+
};
36+
acc.insert(k, v);
37+
acc
38+
})
39+
}

src/utils/value/macros.rs

+144
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
#![allow(unused_macros)]
2+
3+
macro_rules! forward_val_val_binop {
4+
(impl $imp:ident for $res:ty, $method:ident) => {
5+
impl $imp<$res> for $res {
6+
type Output = $res;
7+
8+
#[inline]
9+
fn $method(self, other: $res) -> $res {
10+
// forward to val-ref
11+
$imp::$method(self, &other)
12+
}
13+
}
14+
};
15+
}
16+
17+
macro_rules! forward_ref_val_binop {
18+
(impl $imp:ident for $res:ty, $method:ident) => {
19+
impl $imp<$res> for &$res {
20+
type Output = $res;
21+
22+
#[inline]
23+
fn $method(self, other: $res) -> $res {
24+
// forward to ref-ref
25+
$imp::$method(self, &other)
26+
}
27+
}
28+
};
29+
}
30+
31+
macro_rules! forward_val_ref_binop {
32+
(impl $imp:ident for $res:ty, $method:ident) => {
33+
impl $imp<&$res> for $res {
34+
type Output = $res;
35+
36+
#[inline]
37+
fn $method(self, other: &$res) -> $res {
38+
// forward to ref-ref
39+
$imp::$method(&self, other)
40+
}
41+
}
42+
};
43+
}
44+
45+
macro_rules! forward_scalar_val_val_binop_to_ref_val {
46+
(impl $imp:ident<$scalar:ty> for $res:ty, $method:ident) => {
47+
impl $imp<$scalar> for $res {
48+
type Output = $res;
49+
50+
#[inline]
51+
fn $method(self, other: $scalar) -> $res {
52+
$imp::$method(&self, other)
53+
}
54+
}
55+
56+
impl $imp<$res> for $scalar {
57+
type Output = $res;
58+
59+
#[inline]
60+
fn $method(self, other: $res) -> $res {
61+
$imp::$method(self, &other)
62+
}
63+
}
64+
};
65+
}
66+
67+
macro_rules! forward_scalar_ref_val_binop_to_ref_ref {
68+
(impl $imp:ident<$scalar:ty> for $res:ty, $method:ident) => {
69+
impl $imp<$scalar> for &$res {
70+
type Output = $res;
71+
72+
#[inline]
73+
fn $method(self, other: $scalar) -> $res {
74+
$imp::$method(self, &other)
75+
}
76+
}
77+
78+
impl $imp<$res> for &$scalar {
79+
type Output = $res;
80+
81+
#[inline]
82+
fn $method(self, other: $res) -> $res {
83+
$imp::$method(self, &other)
84+
}
85+
}
86+
};
87+
}
88+
89+
macro_rules! forward_scalar_val_ref_binop_to_ref_val {
90+
(impl $imp:ident<$scalar:ty> for $res:ty, $method:ident) => {
91+
impl $imp<&$scalar> for $res {
92+
type Output = $res;
93+
94+
#[inline]
95+
fn $method(self, other: &$scalar) -> $res {
96+
$imp::$method(&self, other)
97+
}
98+
}
99+
100+
impl $imp<&$res> for $scalar {
101+
type Output = $res;
102+
103+
#[inline]
104+
fn $method(self, other: &$res) -> $res {
105+
$imp::$method(&self, other)
106+
}
107+
}
108+
};
109+
}
110+
111+
macro_rules! forward_scalar_ref_ref_binop_commutative {
112+
(impl $imp:ident<$scalar:ty> for $res:ty, $method:ident) => {
113+
impl $imp<&$res> for &$scalar {
114+
type Output = $res;
115+
116+
#[inline]
117+
fn $method(self, other: &$res) -> $res {
118+
$imp::$method(other, self)
119+
}
120+
}
121+
};
122+
}
123+
124+
macro_rules! forward_into_bigint_scalar_ref_val_binop_to_ref_ref {
125+
(impl $imp:ident<$scalar:ty> for $res:ty, $method:ident) => {
126+
impl $imp<$scalar> for &$res {
127+
type Output = $res;
128+
129+
#[inline]
130+
fn $method(self, other: $scalar) -> $res {
131+
$imp::$method(self, &BigInt::from(other))
132+
}
133+
}
134+
135+
impl $imp<&$res> for $scalar {
136+
type Output = $res;
137+
138+
#[inline]
139+
fn $method(self, other: &$res) -> $res {
140+
$imp::$method(&BigInt::from(self), other)
141+
}
142+
}
143+
};
144+
}

src/utils/value/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#[macro_use]
2+
pub mod macros;
3+
pub mod value_utils;

0 commit comments

Comments
 (0)