Debian Codegraph @

src/lib.rs

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 #![no_std]
2
3 extern crate libcrust-analyzer cargo malloc_buf 1.0.0 libc/;
4 #[cfg(test)]
5 extern crate stdrust-analyzer cargo malloc_buf 1.0.0 std/;
6
7 use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/;
8 use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::Derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#;
9 use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/;
10 use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/;
11 use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/::{Utf8Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/error/Utf8Error#, selfrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/};
12 use libcrust-analyzer cargo libc 0.2.189 crate/::{c_charrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_char#, c_voidrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_void#};
13
•14 const DUMMY_PTRrust-analyzer cargo malloc_buf 1.0.0 DUMMY_PTR.: *mut c_voidrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_void# = 0x10 as *mut c_voidrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_void#;
15
16 /// A type that represents a `malloc`'d chunk of memory.
17 /// When dropped, a `Malloc` will `free` that memory.
18 pub struct Mallocrust-analyzer cargo malloc_buf 1.0.0 Malloc#<T: ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#> {
19 ptrrust-analyzer cargo malloc_buf 1.0.0 Malloc#ptr.: *mut T,
20 }
21
22 impl<T> Mallocrust-analyzer cargo malloc_buf 1.0.0 Malloc#<T> {
23 /**
24 Constructs a new `Malloc` for a `malloc`'d value.
25
26 Unsafe because there must be a valid instance of `T` at `ptr`.
27 */
28 pub unsafe fn from_ptrrust-analyzer cargo malloc_buf 1.0.0 impl#[`Malloc<T>`]from_ptr().(ptr: *mut T) -> Mallocrust-analyzer cargo malloc_buf 1.0.0 Malloc#<T> {
29 Mallocrust-analyzer cargo malloc_buf 1.0.0 Malloc# { ptrrust-analyzer cargo malloc_buf 1.0.0 Malloc#ptr.: ptr }
30 }
31 }
32
33 impl<T> Mallocrust-analyzer cargo malloc_buf 1.0.0 Malloc#<[T]> {
34 /**
35 Constructs a new `Malloc` for a `malloc`'d buffer with the given length
36 at the given pointer.
37
38 Unsafe because there must be `len` contiguous, valid instances of `T`
39 at `ptr`.
40
41 The given pointer must not be null unless the length is 0; this function
42 will specially handle null, 0-length buffers safely.
43 */
44 pub unsafe fn from_arrayrust-analyzer cargo malloc_buf 1.0.0 impl#[`Malloc<[T]>`]from_array().(ptr: *mut T, len: usize) -> Mallocrust-analyzer cargo malloc_buf 1.0.0 Malloc#<[T]> {
45 // Even a 0-size slice cannot be null, so just use another pointer
46 let ptr = if ptr.is_nullrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]is_null().() && len rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().0 { DUMMY_PTRrust-analyzer cargo malloc_buf 1.0.0 DUMMY_PTR. as *mut T }
47 else { ptr };
48 let slice = slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/::from_raw_partsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/raw/from_raw_parts().(ptr, len);
49 Mallocrust-analyzer cargo malloc_buf 1.0.0 Malloc# { ptrrust-analyzer cargo malloc_buf 1.0.0 Malloc#ptr.: slice as *const [T] as *mut [T] }
50 }
51 }
52
53 impl Mallocrust-analyzer cargo malloc_buf 1.0.0 Malloc#<str> {
54 /**
55 Constructs a new `Malloc` for a `malloc`'d nul-terminated C string
56 at the given pointer. Returns an error if the bytes are not valid UTF8.
57
58 Unsafe because `ptr` must point to valid, nul-terminated bytes.
59
60 Once created, when dereferenced the `Malloc<str>` will return the string
61 without its nul-terminator.
62 */
63 pub unsafe fn from_c_strrust-analyzer cargo malloc_buf 1.0.0 impl#[`Malloc<str>`]from_c_str().(ptr: *mut c_charrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_char#)
64 -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Mallocrust-analyzer cargo malloc_buf 1.0.0 Malloc#<str>, Utf8Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/error/Utf8Error#> {
65 let len = libcrust-analyzer cargo libc 0.2.189 crate/::strlenrust-analyzer cargo libc 0.2.189 strlen().(ptr);
66 let slice = slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/::from_raw_partsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/raw/from_raw_parts().(ptr as *mut u8, len);
67 strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/::from_utf8rust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/converts/from_utf8().(slice).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(|s| {
68 Mallocrust-analyzer cargo malloc_buf 1.0.0 Malloc# { ptrrust-analyzer cargo malloc_buf 1.0.0 Malloc#ptr.: s as *const str as *mut str }
69 })
70 }
71 }
72
73 impl<T: ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#> Derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref# for Mallocrust-analyzer cargo malloc_buf 1.0.0 Malloc#<T> {
74 type Targetrust-analyzer cargo malloc_buf 1.0.0 impl#[`Malloc<T>`][Deref]Target# = T;
75
76 fn derefrust-analyzer cargo malloc_buf 1.0.0 impl#[`Malloc<T>`][Deref]deref().(&self) -> &T {
77 unsafe { &*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#deref().self.ptrrust-analyzer cargo malloc_buf 1.0.0 Malloc#ptr. }
78 }
79 }
80
81 impl<T: ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#> Droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/drop/Drop# for Mallocrust-analyzer cargo malloc_buf 1.0.0 Malloc#<T> {
82 fn droprust-analyzer cargo malloc_buf 1.0.0 impl#[`Malloc<T>`][Drop]drop().(&mut self) {
83 if (self.ptrrust-analyzer cargo malloc_buf 1.0.0 Malloc#ptr. as *mut c_voidrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_void#) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().DUMMY_PTRrust-analyzer cargo malloc_buf 1.0.0 DUMMY_PTR. {
84 unsafe {
85 ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::drop_in_placerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/drop_in_place().(self.ptrrust-analyzer cargo malloc_buf 1.0.0 Malloc#ptr.);
86 libcrust-analyzer cargo libc 0.2.189 crate/::freerust-analyzer cargo libc 0.2.189 free().(self.ptrrust-analyzer cargo malloc_buf 1.0.0 Malloc#ptr. as *mut c_voidrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_void#);
87 }
88 }
89 }
90 }
91
92 impl<T: fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug# + ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#> fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug# for Mallocrust-analyzer cargo malloc_buf 1.0.0 Malloc#<T> {
93 fn fmtrust-analyzer cargo malloc_buf 1.0.0 impl#[`Malloc<T>`][Debug]fmt().(&self, f: &mut fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
94 fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug#::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug#fmt().(&*rust-analyzer cargo malloc_buf 1.0.0 impl#[`Malloc<T>`][Deref]deref().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().self, f)
95 }
96 }
97
98 impl<T: fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# + ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#> fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for Mallocrust-analyzer cargo malloc_buf 1.0.0 Malloc#<T> {
99 fn fmtrust-analyzer cargo malloc_buf 1.0.0 impl#[`Malloc<T>`][Display]fmt().(&self, f: &mut fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
100 fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#fmt().(&*rust-analyzer cargo malloc_buf 1.0.0 impl#[`Malloc<T>`][Deref]deref().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().self, f)
101 }
102 }
103
104 impl<T: ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#> AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<T> for Mallocrust-analyzer cargo malloc_buf 1.0.0 Malloc#<T> {
105 fn as_refrust-analyzer cargo malloc_buf 1.0.0 impl#[`Malloc<T>`][`AsRef<T>`]as_ref().(&self) -> &T {
106 &*rust-analyzer cargo malloc_buf 1.0.0 impl#[`Malloc<T>`][Deref]deref().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().self
107 }
108 }
109
110 #[cfg(test)]
111 mod testsrust-analyzer cargo malloc_buf 1.0.0 tests/ {
112 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/;
113 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/;
114 use libcrust-analyzer cargo libc 0.2.189 crate/::{c_charrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_char#, selfrust-analyzer cargo libc 0.2.189 crate/};
115
116 use superrust-analyzer cargo malloc_buf 1.0.0 crate/::Mallocrust-analyzer cargo malloc_buf 1.0.0 Malloc#;
117
118 fn allocrust-analyzer cargo malloc_buf 1.0.0 tests/alloc().<T>(value: T) -> *mut T {
119 unsafe {
120 let ptr = libcrust-analyzer cargo libc 0.2.189 crate/::mallocrust-analyzer cargo libc 0.2.189 malloc().(memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T>()) as *mut T;
121 ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/write().(ptr, value);
122 ptr
123 }
124 }
125
126 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
127 fn test_null_bufrust-analyzer cargo malloc_buf 1.0.0 tests/test_null_buf().() {
128 let buf = unsafe {
129 Mallocrust-analyzer cargo malloc_buf 1.0.0 Malloc#::<[u32]>::from_arrayrust-analyzer cargo malloc_buf 1.0.0 impl#[`Malloc<[T]>`]from_array().(ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::null_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/null_mut().(), 0)
130 };
131 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(&*rust-analyzer cargo malloc_buf 1.0.0 impl#[`Malloc<T>`][Deref]deref().buf == []);
132 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(&*rust-analyzer cargo malloc_buf 1.0.0 impl#[`Malloc<T>`][Deref]deref().buf) == Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(&[]));
133 }
134
135 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
136 fn test_bufrust-analyzer cargo malloc_buf 1.0.0 tests/test_buf().() {
137 let ptr = allocrust-analyzer cargo malloc_buf 1.0.0 tests/alloc().([1, 2, 3]);
138 let buf = unsafe { Mallocrust-analyzer cargo malloc_buf 1.0.0 Malloc#::from_arrayrust-analyzer cargo malloc_buf 1.0.0 impl#[`Malloc<[T]>`]from_array().(ptr as *mut i32, 3) };
139 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(&*rust-analyzer cargo malloc_buf 1.0.0 impl#[`Malloc<T>`][Deref]deref().buf == [1, 2, 3]);
140 }
141
142 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
143 fn test_stringrust-analyzer cargo malloc_buf 1.0.0 tests/test_string().() {
144 let ptr = allocrust-analyzer cargo malloc_buf 1.0.0 tests/alloc().(['h' as c_charrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_char#, 'e' as c_charrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_char#, 'y' as c_charrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_char#, '\0' as c_charrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_char#]);
145 let s = unsafe { Mallocrust-analyzer cargo malloc_buf 1.0.0 Malloc#::from_c_strrust-analyzer cargo malloc_buf 1.0.0 impl#[`Malloc<str>`]from_c_str().(ptr as *mut c_charrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_char#).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().() };
146 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(&*rust-analyzer cargo malloc_buf 1.0.0 impl#[`Malloc<T>`][Deref]deref().s == "hey");
147 }
148
149 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
150 fn test_singlerust-analyzer cargo malloc_buf 1.0.0 tests/test_single().() {
151 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/::ToStringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/ToString#;
152
153 let m = unsafe { Mallocrust-analyzer cargo malloc_buf 1.0.0 Malloc#::from_ptrrust-analyzer cargo malloc_buf 1.0.0 impl#[`Malloc<T>`]from_ptr().(allocrust-analyzer cargo malloc_buf 1.0.0 tests/alloc().(4)) };
154 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(&*rust-analyzer cargo malloc_buf 1.0.0 impl#[`Malloc<T>`][Deref]deref().m == &4);
155
156 let m = unsafe { Mallocrust-analyzer cargo malloc_buf 1.0.0 Malloc#::from_ptrrust-analyzer cargo malloc_buf 1.0.0 impl#[`Malloc<T>`]from_ptr().(allocrust-analyzer cargo malloc_buf 1.0.0 tests/alloc().("hello".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().())) };
157 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(&*rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][Deref]deref().*rust-analyzer cargo malloc_buf 1.0.0 impl#[`Malloc<T>`][Deref]deref().m == "hello");
158 }
159
160 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
161 fn test_droprust-analyzer cargo malloc_buf 1.0.0 tests/test_drop().() {
162 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::rcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/::Rcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/Rc#;
163
164 let num: Rcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/Rc#<i32> = Rcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/Rc#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/impl#[`Rc<T>`]new().(4);
165 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(Rcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/Rc#::strong_countrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/impl#[`Rc<T, A>`]strong_count().(&num), 1);
166
167 let ptr = allocrust-analyzer cargo malloc_buf 1.0.0 tests/alloc().([num.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/impl#[`Rc<T, A>`][Clone]clone().(), num.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/impl#[`Rc<T, A>`][Clone]clone().()]);
168 let buf = unsafe { Mallocrust-analyzer cargo malloc_buf 1.0.0 Malloc#::from_arrayrust-analyzer cargo malloc_buf 1.0.0 impl#[`Malloc<[T]>`]from_array().(ptr as *mut Rcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/Rc#<i32>, 2) };
169 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(Rcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/Rc#::strong_countrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/impl#[`Rc<T, A>`]strong_count().(&num), 3);
170
171 droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/drop().(buf);
172 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(Rcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/Rc#::strong_countrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/impl#[`Rc<T, A>`]strong_count().(&num), 1);
173 }
174 }