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
7444721d
Commit
7444721d
authored
10 years ago
by
Carl Lerche
Browse files
Options
Downloads
Patches
Plain Diff
Implement Debug for Bytes
parent
e39ba25a
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
src/bytes.rs
+7
-1
7 additions, 1 deletion
src/bytes.rs
src/lib.rs
+40
-0
40 additions, 0 deletions
src/lib.rs
test/test.rs
+1
-0
1 addition, 0 deletions
test/test.rs
test/test_bytes.rs
+42
-0
42 additions, 0 deletions
test/test_bytes.rs
with
90 additions
and
1 deletion
src/bytes.rs
+
7
−
1
View file @
7444721d
use
{
Buf
,
ByteStr
,
ByteBuf
,
SmallByteStr
};
use
std
::{
mem
,
ops
,
ptr
};
use
std
::{
fmt
,
mem
,
ops
,
ptr
};
use
std
::
any
::{
Any
,
TypeId
};
use
std
::
raw
::
TraitObject
;
use
core
::
nonzero
::
NonZero
;
...
...
@@ -165,6 +165,12 @@ impl ops::Index<usize> for Bytes {
}
}
impl
fmt
::
Debug
for
Bytes
{
fn
fmt
(
&
self
,
fmt
:
&
mut
fmt
::
Formatter
)
->
fmt
::
Result
{
super
::
debug
(
self
,
"Bytes"
,
fmt
)
}
}
impl
Clone
for
Bytes
{
fn
clone
(
&
self
)
->
Bytes
{
self
.obj
()
.clone
()
...
...
This diff is collapsed.
Click to expand it.
src/lib.rs
+
40
−
0
View file @
7444721d
...
...
@@ -455,3 +455,43 @@ pub enum BufError {
Underflow
,
Overflow
,
}
/*
*
* ===== Internal utilities =====
*
*/
fn
debug
<
B
:
ByteStr
>
(
bytes
:
&
B
,
name
:
&
str
,
fmt
:
&
mut
fmt
::
Formatter
)
->
fmt
::
Result
{
let
mut
buf
=
bytes
.buf
();
try
!
(
write!
(
fmt
,
"{}[len={}; "
,
name
,
bytes
.len
()));
let
mut
rem
=
128
;
while
let
Some
(
byte
)
=
buf
.read_byte
()
{
if
rem
>
0
{
if
is_ascii
(
byte
)
{
try
!
(
write!
(
fmt
,
"{}"
,
byte
as
char
));
}
else
{
try
!
(
write!
(
fmt
,
"
\\
x{:02X}"
,
byte
));
}
rem
-=
1
;
}
else
{
try
!
(
write!
(
fmt
,
" ... "
));
break
;
}
}
try
!
(
write!
(
fmt
,
"]"
));
Ok
(())
}
fn
is_ascii
(
byte
:
u8
)
->
bool
{
match
byte
{
10
|
13
|
32
...
126
=>
true
,
_
=>
false
,
}
}
This diff is collapsed.
Click to expand it.
test/test.rs
+
1
−
0
View file @
7444721d
...
...
@@ -6,6 +6,7 @@ extern crate bytes;
extern
crate
rand
;
mod
test_byte_buf
;
mod
test_bytes
;
mod
test_rope
;
mod
test_seq_byte_str
;
mod
test_small_byte_str
;
...
...
This diff is collapsed.
Click to expand it.
test/test_bytes.rs
0 → 100644
+
42
−
0
View file @
7444721d
use
bytes
::
*
;
#[test]
pub
fn
test_debug_short_str_valid_ascii
()
{
let
b
=
Bytes
::
from_slice
(
b"abcdefghij234"
);
let
d
=
format!
(
"{:?}"
,
b
);
assert_eq!
(
d
.as_slice
(),
"Bytes[len=13; abcdefghij234]"
);
}
#[test]
pub
fn
test_debug_long_str_valid_ascii
()
{
let
s
=
"Lorem ipsum dolor sit amet, consectetur adipiscing elit.
\
Duis volutpat eros in gravida malesuada. Phasellus lobortis
\
maximus cursus. Praesent tristique orci non purus porta
\
dapibus. Ut ut commodo risus, sed semper felis. Phasellus
\
bibendum dui nunc, ac pharetra dui viverra a. Nunc imperdiet
\
sed nulla ut condimentum. In hac habitasse platea dictumst.
\
Interdum et malesuada fames ac ante ipsum primis in faucibus.
\
Sed facilisis dictum malesuada. Sed tempor odio ullamcorper mi
\
iaculis, eu tempus diam semper. Vivamus pulvinar metus ac erat
\
aliquet aliquam."
;
let
b
=
Bytes
::
from_slice
(
s
.as_bytes
());
let
d
=
format!
(
"{:?}"
,
b
);
assert_eq!
(
d
.as_slice
(),
"Bytes[len=556; Lorem ipsum dolor sit amet,
\
consectetur adipiscing elit. Duis volutpat
\
eros in gravida malesuada. Phasellus
\
lobortis maximus cur ... ]"
);
}
#[test]
pub
fn
test_short_string_invalid_ascii
()
{
let
b
=
Bytes
::
from_slice
(
b"foo
\x00
bar
\xFF
baz"
);
let
d
=
format!
(
"{:?}"
,
b
);
println!
(
"{:?}"
,
b
);
assert_eq!
(
d
.as_slice
(),
"Bytes[len=11; foo
\\
x00bar
\\
xFFbaz]"
);
}
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