Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
bytes-sgx
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
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
Markus Becker
bytes-sgx
Commits
16b4266c
Commit
16b4266c
authored
8 years ago
by
Carl Lerche
Browse files
Options
Downloads
Patches
Plain Diff
Improve Buf/MutBuf impl for Cursor
parent
b6a424d8
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/buf/mod.rs
+27
-21
27 additions, 21 deletions
src/buf/mod.rs
test/test_buf.rs
+4
-1
4 additions, 1 deletion
test/test_buf.rs
with
31 additions
and
22 deletions
src/buf/mod.rs
+
27
−
21
View file @
16b4266c
...
...
@@ -321,23 +321,46 @@ impl fmt::Debug for Box<Buf+Send+'static> {
}
}
impl
Buf
for
io
::
Cursor
<
Vec
<
u8
>
>
{
impl
<
T
:
AsRef
<
[
u8
]
>>
Buf
for
io
::
Cursor
<
T
>
{
fn
remaining
(
&
self
)
->
usize
{
self
.get_ref
()
.len
()
-
self
.position
()
as
usize
self
.get_ref
()
.
as_ref
()
.
len
()
-
self
.position
()
as
usize
}
fn
bytes
(
&
self
)
->
&
[
u8
]
{
let
pos
=
self
.position
()
as
usize
;
&
(
&
self
.get_ref
())[
pos
..
]
&
(
self
.get_ref
()
.as_ref
()
)[
pos
..
]
}
fn
advance
(
&
mut
self
,
cnt
:
usize
)
{
let
pos
=
self
.position
()
as
usize
;
let
pos
=
cmp
::
min
(
self
.get_ref
()
.len
(),
pos
+
cnt
);
let
pos
=
cmp
::
min
(
self
.get_ref
()
.
as_ref
()
.
len
(),
pos
+
cnt
);
self
.set_position
(
pos
as
u64
);
}
}
impl
<
T
:
AsMut
<
[
u8
]
>
+
AsRef
<
[
u8
]
>>
MutBuf
for
io
::
Cursor
<
T
>
{
fn
remaining
(
&
self
)
->
usize
{
self
.get_ref
()
.as_ref
()
.len
()
-
self
.position
()
as
usize
}
/// Advance the internal cursor of the MutBuf
unsafe
fn
advance
(
&
mut
self
,
cnt
:
usize
)
{
let
pos
=
self
.position
()
as
usize
;
let
pos
=
cmp
::
min
(
self
.get_mut
()
.as_mut
()
.len
(),
pos
+
cnt
);
self
.set_position
(
pos
as
u64
);
}
/// Returns a mutable slice starting at the current MutBuf position and of
/// length between 0 and `MutBuf::remaining()`.
///
/// The returned byte slice may represent uninitialized memory.
unsafe
fn
mut_bytes
<
'a
>
(
&
'a
mut
self
)
->
&
'a
mut
[
u8
]
{
let
pos
=
self
.position
()
as
usize
;
&
mut
(
self
.get_mut
()
.as_mut
())[
pos
..
]
}
}
impl
MutBuf
for
Vec
<
u8
>
{
fn
remaining
(
&
self
)
->
usize
{
usize
::
MAX
-
self
.len
()
...
...
@@ -371,23 +394,6 @@ impl MutBuf for Vec<u8> {
}
}
impl
<
'a
>
Buf
for
io
::
Cursor
<&
'a
[
u8
]
>
{
fn
remaining
(
&
self
)
->
usize
{
self
.get_ref
()
.len
()
-
self
.position
()
as
usize
}
fn
bytes
(
&
self
)
->
&
[
u8
]
{
let
pos
=
self
.position
()
as
usize
;
&
(
&
self
.get_ref
())[
pos
..
]
}
fn
advance
(
&
mut
self
,
cnt
:
usize
)
{
let
pos
=
self
.position
()
as
usize
;
let
pos
=
cmp
::
min
(
self
.get_ref
()
.len
(),
pos
+
cnt
);
self
.set_position
(
pos
as
u64
);
}
}
/*
*
* ===== Read impls =====
...
...
This diff is collapsed.
Click to expand it.
test/test_buf.rs
+
4
−
1
View file @
16b4266c
use
bytes
::{
Buf
,
MutBuf
};
use
std
::
usize
;
use
std
::
io
::{
Cursor
};
#[test]
pub
fn
test_fresh_cursor_vec
()
{
use
bytes
::
Buf
;
let
mut
buf
=
Cursor
::
new
(
b"hello"
.to_vec
());
assert_eq!
(
buf
.remaining
(),
5
);
...
...
@@ -27,6 +28,8 @@ pub fn test_fresh_cursor_vec() {
#[test]
pub
fn
test_vec_as_mut_buf
()
{
use
bytes
::
MutBuf
;
let
mut
buf
=
Vec
::
with_capacity
(
64
);
assert_eq!
(
buf
.remaining
(),
usize
::
MAX
);
...
...
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