Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
H
heapless
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
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Per Lindgren
heapless
Commits
0917beb9
Commit
0917beb9
authored
7 years ago
by
Alex Helfet
Browse files
Options
Downloads
Patches
Plain Diff
Add method Vec::resize_default().
parent
4b339468
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/vec.rs
+25
-0
25 additions, 0 deletions
src/vec.rs
with
25 additions
and
0 deletions
src/vec.rs
+
25
−
0
View file @
0917beb9
...
...
@@ -92,6 +92,8 @@ where
/// If new_len is greater than len, the Vec is extended by the
/// difference, with each additional slot filled with value. If
/// new_len is less than len, the Vec is simply truncated.
///
/// See also [`resize_default`].
pub
fn
resize
(
&
mut
self
,
new_len
:
usize
,
value
:
T
)
->
Result
<
(),
BufferFullError
>
where
T
:
Clone
{
...
...
@@ -109,6 +111,19 @@ where
Ok
(())
}
/// Resizes the `Vec` in-place so that `len` is equal to `new_len`.
///
/// If `new_len` is greater than `len`, the `Vec` is extended by the
/// difference, with each additional slot filled with `Default::default()`.
/// If `new_len` is less than `len`, the `Vec` is simply truncated.
///
/// See also [`resize`].
pub
fn
resize_default
(
&
mut
self
,
new_len
:
usize
)
->
Result
<
(),
BufferFullError
>
where
T
:
Clone
+
Default
{
self
.resize
(
new_len
,
T
::
default
())
}
}
impl
<
T
,
A
>
Drop
for
Vec
<
T
,
A
>
...
...
@@ -335,4 +350,14 @@ mod tests {
v
.resize
(
1
,
0
)
.unwrap
();
assert_eq!
(
v
[
0
],
17
);
}
#[test]
fn
resize_default
()
{
let
mut
v
:
Vec
<
u8
,
[
u8
;
4
]
>
=
Vec
::
new
();
// resize_default is implemented using resize, so just check the
// correct value is being written.
v
.resize_default
(
1
)
.unwrap
();
assert_eq!
(
v
[
0
],
0
);
}
}
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