forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 435
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #87 from adamrk/rust-int-params
Add Rust int param types
- Loading branch information
Showing
11 changed files
with
399 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
|
||
//! Struct for writing to a pre-allocated buffer with the [`write!`] macro. | ||
use core::fmt; | ||
|
||
/// A pre-allocated buffer that implements [`core::fmt::Write`]. | ||
/// | ||
/// Consequtive writes will append to what has already been written. | ||
/// Writes that don't fit in the buffer will fail. | ||
pub struct Buffer<'a> { | ||
slice: &'a mut [u8], | ||
pos: usize, | ||
} | ||
|
||
impl<'a> Buffer<'a> { | ||
/// Create a new buffer from an existing array. | ||
pub fn new(slice: &'a mut [u8]) -> Self { | ||
Buffer { slice, pos: 0 } | ||
} | ||
|
||
/// Number of bytes that have already been written to the buffer. | ||
/// This will always be less than the length of the original array. | ||
pub fn bytes_written(&self) -> usize { | ||
self.pos | ||
} | ||
} | ||
|
||
impl<'a> fmt::Write for Buffer<'a> { | ||
fn write_str(&mut self, s: &str) -> fmt::Result { | ||
if s.len() > self.slice.len() - self.pos { | ||
Err(fmt::Error) | ||
} else { | ||
self.slice[self.pos..self.pos + s.len()].copy_from_slice(s.as_bytes()); | ||
self.pos += s.len(); | ||
Ok(()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.