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
d05bfb63
"README.md" did not exist on "ac2c0ff24098b79fe40b49f4d74d5a5a89231c59"
Commit
d05bfb63
authored
8 years ago
by
Carl Lerche
Browse files
Options
Downloads
Patches
Plain Diff
Add more Buf helpers
parent
3c58b0c7
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
+99
-0
99 additions, 0 deletions
src/buf/mod.rs
src/lib.rs
+1
-1
1 addition, 1 deletion
src/lib.rs
with
100 additions
and
1 deletion
src/buf/mod.rs
+
99
−
0
View file @
d05bfb63
...
@@ -166,10 +166,20 @@ pub trait Buf {
...
@@ -166,10 +166,20 @@ pub trait Buf {
T
::
read_f64
(
&
buf
)
T
::
read_f64
(
&
buf
)
}
}
/// Creates a "by reference" adaptor for this instance of Buf
fn
by_ref
(
&
mut
self
)
->
&
mut
Self
where
Self
:
Sized
{
self
}
/// Create an adapter which will limit at most `limit` bytes from it.
/// Create an adapter which will limit at most `limit` bytes from it.
fn
take
(
self
,
limit
:
usize
)
->
Take
<
Self
>
where
Self
:
Sized
{
fn
take
(
self
,
limit
:
usize
)
->
Take
<
Self
>
where
Self
:
Sized
{
Take
::
new
(
self
,
limit
)
Take
::
new
(
self
,
limit
)
}
}
/// Return a `Reader` for the value. Allows using a `Buf` as an `io::Read`
fn
reader
(
self
)
->
Reader
<
Self
>
where
Self
:
Sized
{
Reader
::
new
(
self
)
}
}
}
/// A trait for values that provide sequential write access to bytes.
/// A trait for values that provide sequential write access to bytes.
...
@@ -335,10 +345,21 @@ pub trait MutBuf {
...
@@ -335,10 +345,21 @@ pub trait MutBuf {
self
.write_slice
(
&
buf
)
self
.write_slice
(
&
buf
)
}
}
/// Creates a "by reference" adaptor for this instance of MutBuf
fn
by_ref
(
&
mut
self
)
->
&
mut
Self
where
Self
:
Sized
{
self
}
/// Create an adapter which will limit at most `limit` bytes from it.
/// Create an adapter which will limit at most `limit` bytes from it.
fn
take
(
self
,
limit
:
usize
)
->
Take
<
Self
>
where
Self
:
Sized
{
fn
take
(
self
,
limit
:
usize
)
->
Take
<
Self
>
where
Self
:
Sized
{
Take
::
new
(
self
,
limit
)
Take
::
new
(
self
,
limit
)
}
}
/// Return a `Write` for the value. Allows using a `MutBuf` as an
/// `io::Write`
fn
writer
(
self
)
->
Writer
<
Self
>
where
Self
:
Sized
{
Writer
::
new
(
self
)
}
}
}
/*
/*
...
@@ -442,6 +463,43 @@ impl<'a> Sink for &'a mut Vec<u8> {
...
@@ -442,6 +463,43 @@ impl<'a> Sink for &'a mut Vec<u8> {
*
*
*/
*/
/// Adapts a `Buf` to the `io::Read` trait
pub
struct
Reader
<
B
>
{
buf
:
B
,
}
impl
<
B
:
Buf
>
Reader
<
B
>
{
/// Return a `Reader` for the given `buf`
pub
fn
new
(
buf
:
B
)
->
Reader
<
B
>
{
Reader
{
buf
:
buf
}
}
/// Gets a reference to the underlying buf.
pub
fn
get_ref
(
&
self
)
->
&
B
{
&
self
.buf
}
/// Gets a mutable reference to the underlying buf.
pub
fn
get_mut
(
&
mut
self
)
->
&
mut
B
{
&
mut
self
.buf
}
/// Unwraps this `Reader`, returning the underlying `Buf`
pub
fn
into_inner
(
self
)
->
B
{
self
.buf
}
}
impl
<
B
:
Buf
+
Sized
>
io
::
Read
for
Reader
<
B
>
{
fn
read
(
&
mut
self
,
dst
:
&
mut
[
u8
])
->
io
::
Result
<
usize
>
{
let
len
=
cmp
::
min
(
self
.buf
.remaining
(),
dst
.len
());
Buf
::
copy_to
(
&
mut
self
.buf
,
&
mut
dst
[
0
..
len
]);
Ok
(
len
)
}
}
/// Buffer related extension for `io::Read`
pub
trait
ReadExt
{
pub
trait
ReadExt
{
fn
read_buf
<
B
:
MutBuf
>
(
&
mut
self
,
buf
:
&
mut
B
)
->
io
::
Result
<
usize
>
;
fn
read_buf
<
B
:
MutBuf
>
(
&
mut
self
,
buf
:
&
mut
B
)
->
io
::
Result
<
usize
>
;
}
}
...
@@ -461,6 +519,47 @@ impl<T: io::Read> ReadExt for T {
...
@@ -461,6 +519,47 @@ impl<T: io::Read> ReadExt for T {
}
}
}
}
/// Adapts a `MutBuf` to the `io::Write` trait
pub
struct
Writer
<
B
>
{
buf
:
B
,
}
impl
<
B
:
MutBuf
>
Writer
<
B
>
{
/// Return a `Writer` for teh given `buf`
pub
fn
new
(
buf
:
B
)
->
Writer
<
B
>
{
Writer
{
buf
:
buf
}
}
/// Gets a reference to the underlying buf.
pub
fn
get_ref
(
&
self
)
->
&
B
{
&
self
.buf
}
/// Gets a mutable reference to the underlying buf.
pub
fn
get_mut
(
&
mut
self
)
->
&
mut
B
{
&
mut
self
.buf
}
/// Unwraps this `Writer`, returning the underlying `MutBuf`
pub
fn
into_inner
(
self
)
->
B
{
self
.buf
}
}
impl
<
B
:
MutBuf
+
Sized
>
io
::
Write
for
Writer
<
B
>
{
fn
write
(
&
mut
self
,
src
:
&
[
u8
])
->
io
::
Result
<
usize
>
{
let
n
=
cmp
::
min
(
self
.buf
.remaining
(),
src
.len
());
self
.buf
.copy_from
(
&
src
[
0
..
n
]);
Ok
(
n
)
}
fn
flush
(
&
mut
self
)
->
io
::
Result
<
()
>
{
Ok
(())
}
}
/// Buffer related extension for `io::Write`
pub
trait
WriteExt
{
pub
trait
WriteExt
{
fn
write_buf
<
B
:
Buf
>
(
&
mut
self
,
buf
:
&
mut
B
)
->
io
::
Result
<
usize
>
;
fn
write_buf
<
B
:
Buf
>
(
&
mut
self
,
buf
:
&
mut
B
)
->
io
::
Result
<
usize
>
;
}
}
...
...
This diff is collapsed.
Click to expand it.
src/lib.rs
+
1
−
1
View file @
d05bfb63
...
@@ -11,7 +11,7 @@ mod bytes;
...
@@ -11,7 +11,7 @@ mod bytes;
pub
mod
alloc
;
pub
mod
alloc
;
pub
use
buf
::{
Buf
,
MutBuf
,
Source
,
Sink
,
Read
Ext
,
WriteExt
,
Fmt
};
pub
use
buf
::{
Buf
,
MutBuf
,
Source
,
Sink
,
Read
er
,
ReadExt
,
Writer
,
WriteExt
,
Fmt
};
pub
use
buf
::
append
::
AppendBuf
;
pub
use
buf
::
append
::
AppendBuf
;
pub
use
buf
::
block
::{
BlockBuf
,
BlockBufCursor
};
pub
use
buf
::
block
::{
BlockBuf
,
BlockBufCursor
};
pub
use
buf
::
byte
::{
ByteBuf
,
MutByteBuf
};
pub
use
buf
::
byte
::{
ByteBuf
,
MutByteBuf
};
...
...
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