Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
55 changes: 50 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,50 @@ 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 an `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