Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
picodata
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Container Registry
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
core
picodata
Commits
8890e1be
Commit
8890e1be
authored
2 years ago
by
Georgy Moshkin
Browse files
Options
Downloads
Patches
Plain Diff
feat(util): util::Either methods
parent
9aacf61a
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!259
feat(network): arbitrary sync/async rpc requests
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/util.rs
+107
-0
107 additions, 0 deletions
src/util.rs
with
107 additions
and
0 deletions
src/util.rs
+
107
−
0
View file @
8890e1be
pub
use
Either
::{
Left
,
Right
};
////////////////////////////////////////////////////////////////////////////////
/// A generic enum that contains exactly one of two possible types. Equivalent
/// to `std::result::Result`, but is more intuitive in some cases.
#[derive(Debug,
PartialEq,
Eq,
Hash)]
pub
enum
Either
<
L
,
R
>
{
Left
(
L
),
Right
(
R
),
}
impl
<
L
,
R
>
Either
<
L
,
R
>
{
#[inline(always)]
pub
fn
map_left
<
F
,
T
>
(
self
,
f
:
F
)
->
Either
<
T
,
R
>
where
F
:
FnOnce
(
L
)
->
T
,
{
match
self
{
Left
(
l
)
=>
Left
(
f
(
l
)),
Right
(
r
)
=>
Right
(
r
),
}
}
#[inline(always)]
pub
fn
left
(
self
)
->
Option
<
L
>
{
match
self
{
Left
(
l
)
=>
Some
(
l
),
_
=>
None
,
}
}
#[inline(always)]
pub
fn
unwrap_left_or
(
self
,
default
:
L
)
->
L
{
match
self
{
Left
(
l
)
=>
l
,
_
=>
default
,
}
}
#[inline(always)]
pub
fn
unwrap_left_or_else
(
self
,
f
:
impl
FnOnce
(
R
)
->
L
)
->
L
{
match
self
{
Left
(
l
)
=>
l
,
Right
(
r
)
=>
f
(
r
),
}
}
#[inline(always)]
pub
fn
map_right
<
F
,
T
>
(
self
,
f
:
F
)
->
Either
<
L
,
T
>
where
F
:
FnOnce
(
R
)
->
T
,
{
match
self
{
Left
(
l
)
=>
Left
(
l
),
Right
(
r
)
=>
Right
(
f
(
r
)),
}
}
#[inline(always)]
pub
fn
right
(
self
)
->
Option
<
R
>
{
match
self
{
Right
(
r
)
=>
Some
(
r
),
_
=>
None
,
}
}
#[inline(always)]
pub
fn
unwrap_right_or
(
self
,
default
:
R
)
->
R
{
match
self
{
Right
(
r
)
=>
r
,
_
=>
default
,
}
}
#[inline(always)]
pub
fn
unwrap_right_or_else
(
self
,
f
:
impl
FnOnce
(
L
)
->
R
)
->
R
{
match
self
{
Left
(
l
)
=>
f
(
l
),
Right
(
r
)
=>
r
,
}
}
#[inline(always)]
pub
fn
as_ref
(
&
self
)
->
Either
<&
L
,
&
R
>
{
match
self
{
Left
(
l
)
=>
Left
(
l
),
Right
(
r
)
=>
Right
(
r
),
}
}
}
impl
<
L
,
R
>
From
<
Result
<
L
,
R
>>
for
Either
<
L
,
R
>
{
fn
from
(
r
:
Result
<
L
,
R
>
)
->
Self
{
match
r
{
Ok
(
l
)
=>
Left
(
l
),
Err
(
r
)
=>
Right
(
r
),
}
}
}
impl
<
L
,
R
>
From
<
Either
<
L
,
R
>>
for
Result
<
L
,
R
>
{
fn
from
(
e
:
Either
<
L
,
R
>
)
->
Self
{
match
e
{
Left
(
l
)
=>
Ok
(
l
),
Right
(
r
)
=>
Err
(
r
),
}
}
}
////////////////////////////////////////////////////////////////////////////////
// macros
////////////////////////////////////////////////////////////////////////////////
#[macro_export]
macro_rules!
unwrap_ok_or
{
(
$o:expr
,
$err:pat
=>
$
(
$else:tt
)
+
)
=>
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment