-
Notifications
You must be signed in to change notification settings - Fork 13
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
Add support for stable Rust #4
Conversation
Thanks for the PR! This project has been not updated for a long while, so I think it's indeed a good time to review it as the nightly rust had been developed so much. Actually, I've done some refactory work but I didn't push them here. I'm gonna to merge this PR and do the update in one or two days. Again, thanks for bring this up! |
Awesome, thank you! If you need help with anything or would like me to finish this PR, let me know. :) |
Yup, if you have some work not done yet, you can keep submiting them here, I'll do the preliminary work first. |
@stjepang I have to make a decision on the new update and I want to hear your opinion. I'm going to implement a must-success |
So if we resize to a smaller space, we try to turn the Which direction of these two is problematic? I don't understand why we need |
It's quite not efficient to unbox something that are already stored on heap. So
The first problem is that we can't get the content by value(similar to |
I think we could do something like this (this works even on stable Rust 1.28): pub fn resize<ToSpace>(self) -> SmallBox<T, ToSpace> {
use std::mem;
use std::alloc::{alloc, Layout};
match self {
SmallBox::Stack(x) => match x.resize() {
Ok(x) => SmallBox::Stack(x),
Err(x) => {
let size = mem::size_of_val::<T>(&*x);
let align = mem::align_of_val::<T>(&*x);
let boxed = unsafe {
let dest = alloc(Layout::from_size_align_unchecked(size, align));
(&*x as *const T as *const u8).copy_to_nonoverlapping(dest, size);
let mut ptr = &*x as *const T as *mut T;
*(&mut ptr as *mut _ as *mut usize) = dest as usize;
Box::from_raw(ptr)
};
mem::forget(x);
SmallBox::Box(boxed)
}
},
SmallBox::Box(x) => SmallBox::Box(x),
}
} This would be ideal if you don't mind upgrading to Rust 1.28. Crossbeam is still stuck on 1.26, so that might be a bit of a problem for me :/ |
From document:
I am not sure whether we can do this directly... Since the allocate/deallocate behavior is not defined in the source of std but in the compiler as special |
Ah, I missed that - you're right! Seems like we'll have to wait until we get alloca support... In that case, I'd probably prefer not introducing |
Alright, so we had better not to resize into smaller |
And we can't get a |
I came up with an idea just now: we support |
Seems that I can start on this since the unsized ravlue is implemented!(rust-lang/rust#51131) |
Also, we could simply require |
Yup, that's what I am doing now. |
@stjepang The stable part is ready, with updated document, but I encountered some problems on the nightly, and I would be too busy in the following two days to have time on it. The final version should be finished this week. If you need the stable version in a hurry, I can publish a version with dst stripped out now. |
Nah, this week sounds good, take your time :) And thanks for the speedy updates, I really appreciate it! |
@stjepang Version 0.5.0 is available on crates.io now! |
Thank you! |
This is a really neat trick to make unsized types work on stable Rust! I like it :) |
A new flag
nightly
is added that enables nightly features (the most important ones beingCoerceUnsized
andUnsize
). Otherwise, stable Rust is supported butSmallBox
can be used with sized types only.This PR is still a work in progress. More tests need to be ported and the documentation needs to be updated. I'm submitting the PR early to check whether this is a direction we'd like to take at all.
To provide some context, I need
SmallBox
in crossbeam-rs/crossbeam-channel#86