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
e9a70986
Commit
e9a70986
authored
6 years ago
by
Luke Horsley
Committed by
Carl Lerche
6 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Added a resize function for BytesMut (#203)
parent
32ea8281
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/bytes.rs
+41
-0
41 additions, 0 deletions
src/bytes.rs
with
41 additions
and
0 deletions
src/bytes.rs
+
41
−
0
View file @
e9a70986
...
...
@@ -1276,6 +1276,32 @@ impl BytesMut {
self
.truncate
(
0
);
}
/// Resizes the buffer so that `len` is equal to `new_len`.
///
/// If `new_len` is greater than `len`, the buffer is extended by the
/// difference with each additional byte set to `value`. If `new_len` is
/// less than `len`, the buffer is simply truncated.
///
/// # Examples
///
/// ```
/// use bytes::BytesMut;
///
/// let mut buf = BytesMut::new();
///
/// buf.resize(3, 0x1);
/// assert_eq!(&buf[..], &[0x1, 0x1, 0x1]);
///
/// buf.resize(2, 0x2);
/// assert_eq!(&buf[..], &[0x1, 0x1]);
///
/// buf.resize(4, 0x3);
/// assert_eq!(&buf[..], &[0x1, 0x1, 0x3, 0x3]);
/// ```
pub
fn
resize
(
&
mut
self
,
new_len
:
usize
,
value
:
u8
)
{
self
.inner
.resize
(
new_len
,
value
);
}
/// Sets the length of the buffer.
///
/// This will explicitly set the size of the buffer without actually
...
...
@@ -1890,6 +1916,21 @@ impl Inner {
}
}
fn
resize
(
&
mut
self
,
new_len
:
usize
,
value
:
u8
)
{
let
len
=
self
.len
();
if
new_len
>
len
{
let
additional
=
new_len
-
len
;
self
.reserve
(
additional
);
unsafe
{
let
dst
=
self
.as_raw
()[
len
..
]
.as_mut_ptr
();
ptr
::
write_bytes
(
dst
,
value
,
additional
);
self
.set_len
(
new_len
);
}
}
else
{
self
.truncate
(
new_len
);
}
}
unsafe
fn
set_start
(
&
mut
self
,
start
:
usize
)
{
// Setting the start to 0 is a no-op, so return early if this is the
// case.
...
...
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