Skip to content

Add an unsizing operation to {Arc,Rc}Box #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions crates/rc-box/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,10 @@ slice-dst = { version = "1.4.0", optional = true }
version = "1.0.0"
path = "../erasable"
optional = true

[dependencies.unsize]
version = "1.1"
optional = true

[package.metadata.docs.rs]
all-features = true
56 changes: 51 additions & 5 deletions crates/rc-box/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,6 @@ macro_rules! rc_box {

// ~~~ $Rc<T> and Box<T> like inherent impls ~~~ //

// downcast is pretty useless without CoerceUnsized

impl $RcBox<dyn Any + 'static> {
doc_comment! {
concat!("Attempt to downcast the box to a concrete type.
Expand All @@ -122,7 +120,8 @@ print_if_string(my_number.try_into().unwrap());
```

The unsizing as `", stringify!($Rc), "` is required until
[DST coercions](https://github.com/rust-lang/rust/issues/27732) are stabilized."),
[DST coercions](https://github.com/rust-lang/rust/issues/27732) are stabilized. Alternatively,
activate the `unsize` feature to convert the pointer via an explicit method call."),
#[inline]
pub fn downcast<T>(self) -> Result<$RcBox<T>, Self>
where T: Any,
Expand Down Expand Up @@ -164,7 +163,8 @@ print_if_string(my_number.try_into().unwrap());
```

The unsizing as `", stringify!($Rc), "` is required until
[DST coercions](https://github.com/rust-lang/rust/issues/27732) are stabilized."),
[DST coercions](https://github.com/rust-lang/rust/issues/27732) are stabilized. Alternatively,
activate the `unsize` feature to convert the pointer via an explicit method call."),
#[inline]
pub fn downcast<T>(self) -> Result<$RcBox<T>, Self>
where T: Any + Send
Expand Down Expand Up @@ -206,7 +206,8 @@ print_if_string(my_number.try_into().unwrap());
```

The unsizing as `", stringify!($Rc), "` is required until
[DST coercions](https://github.com/rust-lang/rust/issues/27732) are stabilized."),
[DST coercions](https://github.com/rust-lang/rust/issues/27732) are stabilized. Alternatively,
activate the `unsize` feature to convert the pointer via an explicit method call."),
#[inline]
pub fn downcast<T>(self) -> Result<$RcBox<T>, Self>
where T: Any + Send + Sync
Expand Down Expand Up @@ -634,6 +635,51 @@ then the data will be pinned in memory and unable to be moved."),

#[cfg(feature = "std")]
impl<T: ?Sized> UnwindSafe for $RcBox<T> where Box<T>: UnwindSafe {}

#[cfg(feature = "unsize")]
doc_comment! {
concat!("Unsizes a pointer using the `unsize` crate.

# Usage

```
# use rc_box::*;
use unsize::{Coercion, CoerceUnsize};

let unique = ", stringify!($RcBox), "::new(|| 42u32);
let unique:", stringify!($RcBox), r"<dyn Fn() -> u32> =
unique.unsize(Coercion::<_, dyn Fn() -> u32>::to_fn());

let value = (*unique)();
assert_eq!(value, 42);
```

Another common usage would be to create a `dyn Any`.

fn print_if_string(value: ", stringify!($RcBox), r#"<dyn Any>) {
if let Ok(string) = value.downcast::<String>() {
println!("String ({}): {}", string.len(), string);
}
}

let my_string = "Hello World".to_string();
let my_string: "#, stringify!($RcBox), "<dyn Any> = ", stringify!($RcBox), "::new(my_string).unsize(Coercion::to_any());
print_if_string(my_string);
let my_number: ", stringify!($RcBox), "<dyn Any> = ", stringify!($RcBox), "::new(0i8).unsize(Coercion::to_any());
print_if_string(my_number);
```"),
unsafe impl<T, U: ?Sized> unsize::CoerciblePtr<U> for $RcBox<T> {
type Pointee = T;
type Output = $RcBox<U>;
fn as_sized_ptr(&mut self) -> *mut T {
$RcBox::as_raw(self).as_ptr()
}
unsafe fn replace_ptr(self, new: *mut U) -> $RcBox<U> {
let new = $RcBox::into_raw(self).replace_ptr(new);
$RcBox::from_raw(new.as_ptr() as *const U)
}
}
}
)*};
}

Expand Down