File tree 2 files changed +21
-2
lines changed
2 files changed +21
-2
lines changed Original file line number Diff line number Diff line change @@ -14,19 +14,32 @@ pub struct Buffer<'a> {
14
14
}
15
15
16
16
impl < ' a > Buffer < ' a > {
17
- /// Create a new buffer from an existing array.
17
+ /// Creates a new buffer from an existing array.
18
18
pub fn new ( slice : & ' a mut [ u8 ] ) -> Self {
19
19
Buffer { slice, pos : 0 }
20
20
}
21
21
22
+ /// Creates a new buffer from a raw pointer.
23
+ ///
24
+ /// # Safety
25
+ ///
26
+ /// `ptr` must be valid for read and writes, have at least `len` bytes in
27
+ /// size, and remain valid and not be used by other threads for the lifetime
28
+ /// of the returned instance.
29
+ pub unsafe fn from_raw ( ptr : * mut u8 , len : usize ) -> Self {
30
+ // SAFETY: The safety requirements of the function satisfy those of
31
+ // `from_raw_parts_mut`.
32
+ Self :: new ( unsafe { core:: slice:: from_raw_parts_mut ( ptr, len) } )
33
+ }
34
+
22
35
/// Number of bytes that have already been written to the buffer.
23
36
/// This will always be less than the length of the original array.
24
37
pub fn bytes_written ( & self ) -> usize {
25
38
self . pos
26
39
}
27
40
}
28
41
29
- impl < ' a > fmt:: Write for Buffer < ' a > {
42
+ impl fmt:: Write for Buffer < ' _ > {
30
43
fn write_str ( & mut self , s : & str ) -> fmt:: Result {
31
44
if s. len ( ) > self . slice . len ( ) - self . pos {
32
45
Err ( fmt:: Error )
Original file line number Diff line number Diff line change @@ -396,6 +396,12 @@ impl From<LayoutError> for Error {
396
396
}
397
397
}
398
398
399
+ impl From < core:: fmt:: Error > for Error {
400
+ fn from ( _: core:: fmt:: Error ) -> Error {
401
+ Error :: EINVAL
402
+ }
403
+ }
404
+
399
405
/// A [`Result`] with an [`Error`] error type.
400
406
///
401
407
/// To be used as the return type for functions that may fail.
You can’t perform that action at this time.
0 commit comments