Skip to content

Commit 82f1a07

Browse files
0.0.2-alpha: WIP of: unsafe_static_set, unsafe_ref, unsafe_ref_mut, unsafe_ref_set, unsafe_cast
1 parent 15470e5 commit 82f1a07

File tree

4 files changed

+36
-32
lines changed

4 files changed

+36
-32
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
# 0.0.2-alpha
4+
5+
Initial `unsafe_static_set`, `unsafe_ref`, `unsafe_ref_mut`, `unsafe_ref_set`, `unsafe_cast`. All are WIP.
6+
37
# 0.0.1-alpha
48

59
Initial. Only `unsafe_fn!` and `unsafe_method!` macros.

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "prudent"
3-
version = "0.0.1-alpha"
3+
version = "0.0.2-alpha"
44
edition = "2018"
55

66
license = "BSD-2-Clause OR Apache-2.0 OR MIT"

src/lib.rs

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
// This only refuses unsafe code in functions/expressions in this crate, not ones generated by this
44
// crate's macros.
55
#![cfg_attr(not(any(doc, test)), forbid(unsafe_code))]
6-
//#[cfg(doc)]
7-
//extern crate alloc;
6+
#[cfg(doc)]
7+
extern crate alloc;
88

99
/// Invoke am unsafe function.
1010
///
@@ -40,7 +40,8 @@ macro_rules! unsafe_fn {
4040
(~~ $last:expr) => {
4141
($last,)
4242
};
43-
// Commented out: For now, we require the (potentially unsafe) function to have at least 1 argument.
43+
// Commented out: For now, we require the (potentially unsafe) function to have at least 1
44+
// argument.
4445
//
4546
//(~~) => { () };
4647

@@ -85,7 +86,7 @@ macro_rules! unsafe_fn {
8586
/// Invoke an unsafe method. Like [unsafe_fn], but
8687
/// - we accept a receiver `self`
8788
/// - we store `self` in a variable outside of the generated `unsafe {...}`
88-
/// - we don't allow $fn to be an expression (which doesn't work in standard methods calls), but
89+
/// - we do NOT allow $fn to be an expression (which doesn't work in standard methods calls), but
8990
/// only an identifier.
9091
#[macro_export]
9192
macro_rules! unsafe_method {
@@ -188,25 +189,21 @@ macro_rules! unsafe_deref_set {
188189
/// underlying type).
189190
#[macro_export]
190191
macro_rules! unsafe_ref {
191-
($ptr:expr) => {
192-
{
193-
let ptr = $ptr;
194-
let _: *const _ = ptr; // Partial type check: $ptr needs to yield a const pointer
195-
unsafe { &*ptr }
196-
}
197-
};
192+
($ptr:expr) => {{
193+
let ptr = $ptr;
194+
let _: *const _ = ptr; // Partial type check: $ptr needs to yield a const pointer
195+
unsafe { &*ptr }
196+
}};
198197
}
199198

200199
/// Deref a `mut` pointer and yield a `mut` reference (to the same underlying type).
201200
#[macro_export]
202201
macro_rules! unsafe_ref_mut {
203-
($ptr:expr) => {
204-
{
205-
let ptr = $ptr;
206-
let _: *mut _ = ptr; // Partial type check: $ptr needs to yield a mut pointer
207-
unsafe { &mut *ptr }
208-
}
209-
};
202+
($ptr:expr) => {{
203+
let ptr = $ptr;
204+
let _: *mut _ = ptr; // Partial type check: $ptr needs to yield a mut pointer
205+
unsafe { &mut *ptr }
206+
}};
210207
}
211208

212209
/// Assign the given value to the location given in the pointer.
@@ -225,7 +222,9 @@ macro_rules! unsafe_ref_set {
225222
let ptr = $ptr;
226223
let _: *mut _ = ptr; // Partial type check: $ptr needs to yield a mut pointer
227224
let value = $value;
228-
unsafe { *ptr = value; }
225+
unsafe {
226+
*ptr = value;
227+
}
229228
}};
230229
}
231230

@@ -237,11 +236,10 @@ macro_rules! unsafe_ref_set {
237236
macro_rules! unsafe_cast {
238237
($ptr:expr, $t:ty) => {{
239238
let ptr = $ptr;
240-
unsafe { ptr as $ty }
239+
unsafe { &*ptr as $t }
241240
}};
242241
}
243242

244-
245243
//-------------
246244

247245
#[cfg(test)]
@@ -289,8 +287,8 @@ mod fn_method_tests {
289287
}
290288
fn _unsafe_set_x_0_simple_2(value: bool) {
291289
*unsafe {
292-
// @TODO The expression (here: X) could include subexpressions (like array index) and
293-
// those may include `unsafe` code, too!
290+
// @TODO The expression (here: X) could include subexpressions (like array index)
291+
// and those may include `unsafe` code, too!
294292
#[allow(static_mut_refs)]
295293
&mut X[0]
296294
//
@@ -303,7 +301,7 @@ mod fn_method_tests {
303301

304302
// FLEXIBLE: <<~<<~<<
305303
(*&mut unsafe { X })[0] = value;
306-
304+
307305
(*&mut unsafe { X }) = [true, true];
308306
}
309307
fn _unsafe_set_x_0_store_ref(value: bool) {
@@ -349,8 +347,8 @@ mod fn_method_tests {
349347

350348
// @TODO requires a return type
351349
//
352-
// @TODO doesn't work if the static variable is identified with a
353-
// path like `crate::mod_x::mod_y::STATIC_X``
350+
// @TODO doesn't work if the static variable is identified with a path like
351+
// `crate::mod_x::mod_y::STATIC_X``
354352
pub fn _f() -> bool {
355353
#[allow(non_snake_case)]
356354
let X = &unsafe { super::X };
@@ -397,8 +395,7 @@ mod fn_method_tests {
397395
let pt: *mut bool = &mut a as *mut bool;
398396
399397
let a2: bool = unsafe_deref!(pt);
400-
// FAILS:
401-
//unsafe_deref!(pt) = false;
398+
// FAILS: unsafe_deref!(pt) = false;
402399
}
403400
#[test]
404401
fn deref_mut_field_or_method() {
@@ -416,6 +413,9 @@ mod fn_method_tests {
416413
mod casting_tests {
417414
use std::fmt::Display;
418415

416+
#[test]
417+
fn todo() {}
418+
419419
#[test]
420420
fn read_only() {
421421
let a: bool = true;
@@ -434,10 +434,10 @@ mod casting_tests {
434434
// Not allowed:
435435
//
436436
// unsafe { *pt } = false;
437-
unsafe { *pt = false };
437+
unsafe { *pt = false };
438438

439439
// Move to a separate test:
440-
unsafe_ref_set!( pt, false);
440+
unsafe_ref_set!(pt, false);
441441
}
442442

443443
#[test]

0 commit comments

Comments
 (0)