Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
duniter-core
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Monitor
Service Desk
Analyze
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
nodes
rust
duniter-core
Commits
624b09ec
Commit
624b09ec
authored
4 years ago
by
Éloïs
Browse files
Options
Downloads
Patches
Plain Diff
fix(kv_typed): deser empty BTreeSet
parent
56dd9795
No related branches found
No related tags found
No related merge requests found
Pipeline
#12707
failed
4 years ago
Stage: tests
Stage: quality
Changes
3
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
tools/kv_typed/src/as_bytes.rs
+14
-0
14 additions, 0 deletions
tools/kv_typed/src/as_bytes.rs
tools/kv_typed/src/from_bytes.rs
+38
-8
38 additions, 8 deletions
tools/kv_typed/src/from_bytes.rs
tools/kv_typed/tests/test_db_schema.rs
+5
-0
5 additions, 0 deletions
tools/kv_typed/tests/test_db_schema.rs
with
57 additions
and
8 deletions
tools/kv_typed/src/as_bytes.rs
+
14
−
0
View file @
624b09ec
...
@@ -61,6 +61,20 @@ where
...
@@ -61,6 +61,20 @@ where
}
}
}
}
#[cfg(test)]
mod
tests
{
use
super
::
*
;
#[test]
fn
test_btreeset_as_bytes
()
{
BTreeSet
::
<
u64
>
::
new
()
.as_bytes
(|
bytes
|
assert_eq!
(
bytes
,
&
[]));
}
#[test]
fn
test_hashset_as_bytes
()
{
HashSet
::
<
u64
>
::
new
()
.as_bytes
(|
bytes
|
assert_eq!
(
bytes
,
&
[]));
}
}
macro_rules!
impl_as_bytes_for_le_numbers
{
macro_rules!
impl_as_bytes_for_le_numbers
{
(
$
(
$T:ty
),
*
)
=>
{
$
(
(
$
(
$T:ty
),
*
)
=>
{
$
(
impl
AsBytes
for
$T
{
impl
AsBytes
for
$T
{
...
...
This diff is collapsed.
Click to expand it.
tools/kv_typed/src/from_bytes.rs
+
38
−
8
View file @
624b09ec
...
@@ -97,12 +97,16 @@ where
...
@@ -97,12 +97,16 @@ where
type
Err
=
LayoutVerifiedErr
;
type
Err
=
LayoutVerifiedErr
;
fn
from_bytes
(
bytes
:
&
[
u8
])
->
Result
<
Self
,
Self
::
Err
>
{
fn
from_bytes
(
bytes
:
&
[
u8
])
->
Result
<
Self
,
Self
::
Err
>
{
if
bytes
.is_empty
()
{
Ok
(
Self
::
new
())
}
else
{
let
layout_verified
=
zerocopy
::
LayoutVerified
::
<
_
,
[
T
]
>
::
new_slice
(
bytes
)
let
layout_verified
=
zerocopy
::
LayoutVerified
::
<
_
,
[
T
]
>
::
new_slice
(
bytes
)
.ok_or
(
LayoutVerifiedErr
(
stringify!
(
BTreeSet
<
T
>
)))
?
;
.ok_or
(
LayoutVerifiedErr
(
stringify!
(
BTreeSet
<
T
>
)))
?
;
let
slice
=
layout_verified
.into_slice
();
let
slice
=
layout_verified
.into_slice
();
Ok
(
slice
.iter
()
.copied
()
.collect
())
Ok
(
slice
.iter
()
.copied
()
.collect
())
}
}
}
}
}
impl
<
T
>
FromBytes
for
HashSet
<
T
>
impl
<
T
>
FromBytes
for
HashSet
<
T
>
where
where
...
@@ -111,12 +115,16 @@ where
...
@@ -111,12 +115,16 @@ where
type
Err
=
LayoutVerifiedErr
;
type
Err
=
LayoutVerifiedErr
;
fn
from_bytes
(
bytes
:
&
[
u8
])
->
Result
<
Self
,
Self
::
Err
>
{
fn
from_bytes
(
bytes
:
&
[
u8
])
->
Result
<
Self
,
Self
::
Err
>
{
if
bytes
.is_empty
()
{
Ok
(
Self
::
new
())
}
else
{
let
layout_verified
=
zerocopy
::
LayoutVerified
::
<
_
,
[
T
]
>
::
new_slice
(
bytes
)
let
layout_verified
=
zerocopy
::
LayoutVerified
::
<
_
,
[
T
]
>
::
new_slice
(
bytes
)
.ok_or
(
LayoutVerifiedErr
(
stringify!
(
HashSet
<
T
>
)))
?
;
.ok_or
(
LayoutVerifiedErr
(
stringify!
(
HashSet
<
T
>
)))
?
;
let
slice
=
layout_verified
.into_slice
();
let
slice
=
layout_verified
.into_slice
();
Ok
(
slice
.iter
()
.copied
()
.collect
())
Ok
(
slice
.iter
()
.copied
()
.collect
())
}
}
}
}
}
impl
FromBytes
for
IVec
{
impl
FromBytes
for
IVec
{
type
Err
=
std
::
convert
::
Infallible
;
type
Err
=
std
::
convert
::
Infallible
;
...
@@ -125,3 +133,25 @@ impl FromBytes for IVec {
...
@@ -125,3 +133,25 @@ impl FromBytes for IVec {
Ok
(
Self
::
from
(
bytes
))
Ok
(
Self
::
from
(
bytes
))
}
}
}
}
#[cfg(test)]
mod
tests
{
use
super
::
*
;
#[test]
fn
test_btreeset_from_bytes
()
->
Result
<
(),
LayoutVerifiedErr
>
{
assert_eq!
(
<
BTreeSet
<
u64
>
as
FromBytes
>
::
from_bytes
(
&
[])
?
,
BTreeSet
::
<
u64
>
::
new
()
);
Ok
(())
}
#[test]
fn
test_hashset_from_bytes
()
->
Result
<
(),
LayoutVerifiedErr
>
{
assert_eq!
(
<
HashSet
<
u64
>
as
FromBytes
>
::
from_bytes
(
&
[])
?
,
HashSet
::
<
u64
>
::
new
()
);
Ok
(())
}
}
This diff is collapsed.
Click to expand it.
tools/kv_typed/tests/test_db_schema.rs
+
5
−
0
View file @
624b09ec
...
@@ -116,6 +116,11 @@ fn test_db<B: Backend>(db: &TestV1Db<B>) -> KvResult<()> {
...
@@ -116,6 +116,11 @@ fn test_db<B: Backend>(db: &TestV1Db<B>) -> KvResult<()> {
})
?
;
})
?
;
// Test get_ref_slice
// Test get_ref_slice
db
.col4_write
()
.upsert
(
U64BE
(
3
),
BTreeSet
::
new
())
?
;
db
.col4
()
.get_ref_slice
(
&
U64BE
(
3
),
|
numbers
|
{
assert_eq!
(
numbers
,
&
[]);
Ok
(())
})
?
;
db
.col4_write
()
db
.col4_write
()
.upsert
(
U64BE
(
4
),
(
&
[
3
,
2
,
4
,
1
])
.iter
()
.copied
()
.collect
())
?
;
.upsert
(
U64BE
(
4
),
(
&
[
3
,
2
,
4
,
1
])
.iter
()
.copied
()
.collect
())
?
;
db
.col4
()
.get_ref_slice
(
&
U64BE
(
4
),
|
numbers
|
{
db
.col4
()
.get_ref_slice
(
&
U64BE
(
4
),
|
numbers
|
{
...
...
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