Expand description
Machinery to support functions that return unsized values.
Written to support the unsized-vec crate, but is independent of it.
Requires nightly Rust.
Unsized values can take many forms:
-
On stable Rust, values of unsized types like
str,[u8], anddyn Anyare generally encountered behind a pointer, like&strorBox<dyn Any>. -
Nightly Rust provides limited support for passing unsized values by value as arguments to functions, using the
unsized_fn_paramsfeature. There is alsounsized_locals, for storing these values on the stack using alloca. (However, that feature is “incomplete” and this crate doesn’t make use of it). But even with thse two feature gates enabled, functions cannot return unsized values directly. Also, the only way to produce a by-value unsized value in today’s Rust is by dereferencing aBox; this crate provides theunsizemacro to work around this limitation. -
For functions that return unsized values, this crate provides the
Emplacabletype. Functions that want to return a value of typeT, whereTis unsized, return anEmplacable<T, _>instead.Emplacable<T>wraps a closure; that closure contains instructions for writing out aTto a caller-provided region of memory. Other functions accept theEmplacableas an argument and call its contained closure to write out theTto some allocation provided by them. For example, this crate provides thebox_new_withfunction, which turns anEmplacable<T>into aBox<T>.
§Converting between types
| I have | I want | I can use |
|---|---|---|
[i32; 2] | [i32] | unsize |
[i32; 2] | Emplacable<[i32; 2], _> | Into::into |
[i32] | Emplacable<[i32], _> | with_emplacable_for |
[i32] | Box<[i32]> | box_new |
Box<[i32; 2]> | Box<[i32]> | CoerceUnsized |
Box<[i32]> | [i32] | dereference the box with * |
Box<[i32]> | Emplacable<[i32], _> | Into::into |
Vec<i32> | Emplacable<[i32], _> | Into::into |
Emplacable<[i32; 2], _> | [i32; 2] | Emplacable::get |
Emplacable<[i32; 2], _> | Emplacable<[i32], _> | Into::into |
Emplacable<[i32; 2], _> | Emplacable<dyn Debug, _> | Emplacable::unsize |
Emplacable<[i32], _> | Box<[i32]> | box_new_with |
Emplacable<[i32], _> | Vec<i32> | Into::into |
Emplacable<[i32], _> | Rc<[i32]> | Into::into |
Emplacable<[i32], _> | Arc<[i32]> | Into::into |
&[i32] | Box<[i32]> | Into::into |
&[i32] | Emplacable<[i32], _> | Into::into |
You can replace [i32; 2] and [i32] above by any pair of types (T, U)
such that T: Unsize<U>.
§A note on examples
This crate has very few examples, as it provides tools to work with unsized types
but no fun things that use the tools. If you want more usage examples,
check out unsized-vec’s documentation and the examples folder on GitHub.
Macros§
- by_
value_ str - Construct a
strfrom a string literal, in its dereferenced form. - unsize
- Helper for coercing values to unsized types.
Structs§
- Emplacable
- A wrapped closure that you can pass to functions like
box_new_with, that describes how to write a value of typeTto a caller-provided allocation. You can get aTout of anEmplacablethrough functions likebox_new_with. Alternately, you can drop the value of typeTby dropping theEmplacable. Or you can forget the value of typeTwithEmplacable::forget. - Emplacer
- Passed as the last argument to
Emplacableclosures. Wraps a closure that tells the function where to write its return value.
Traits§
Functions§
- arc_new
- Like
Arc::new, butTcan be?Sized. - arc_
new_ with - Like
Arc::new, but takes anEmplacer<T, _>instead ofTdirectly. - arc_pin
- Like
Arc::pin, butTcan be?Sized. - arc_
pin_ with - Like
Arc::pin, but takes anEmplacer<T, _>instead ofTdirectly. - box_new
- Like
Box::new, butTcan be?Sized. - box_
new_ with - Like
Box::new, but takes anEmplacer<T, _>instead ofTdirectly. - box_pin
- Like
Box::pin, butTcan be?Sized. - box_
pin_ with - Like
Box::pin, , but takes anEmplacer<T, _>instead ofTdirectly. - rc_new
- Like
Rc::new, butTcan be?Sized. - rc_
new_ with - Like
Rc::new, but takes anEmplacer<T, _>instead ofTdirectly. - rc_pin
- Like
Rc::pin, butTcan be?Sized. - rc_
pin_ with - Like
Rc::pin, but takes anEmplacer<T, _>instead ofTdirectly. - with_
emplacable_ for - Accepts a possibly-unsized value as a first argument,
turns it into an
Emplacable, and passes the emplacer to the given closure.
Type Aliases§
- Emplacer
Fn - The type of the closure that
Emplacer<'a, T>wraps. - With
Emplacable ForFn EmplacableFnused bywith_emplacable_for.