Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
Logo by Misiasart
Thanks to all individual and corporate sponsors, without whom this work could not exist:
facet provides "const fn" reflection for Rust.
The Facet trait is meant to be derived for every single type in the Rust
ecosystem, and can be used to replace many other derive macros.
pub unsafe
Whereas crates like serde derive code using the heavy syn, facet derives
data with the light and fast unsynn.
That data does not make compile times balloon due to heavy monomorphization. It can be used to reason about types at runtime — which even allows doing specialization.
The SHAPE associated constant fully describes a type:
- Whether it's a struct, an enum, or a scalar
- All fields, variants, offsets, discriminants, memory layouts
- VTable for various standard traits:
- Display, Debug, Clone, Default, Drop etc.
Use case: inspection, pretty printing, debugging, specialization
The Debug trait is severely limited because it cannot be specialized.
facet-pretty provides pretty printing of any type that implements Facet:
let address = Address ;
let person = Person ;
println!;
println!;
)
)
(Note: the default pretty-printing shows ANSI colors).
Facet knows the type inside the T, so it's able to format it:
use FacetPretty;
)
)
Because we know the shape of T, we can format different things differently,
if we wanted to:
let mut file = open.expect;
let mut bytes = vec!;
read_exact.expect;
println!;
)
And because we can make this decision at runtime, it can be an option on the pretty-printer itself:
/// A formatter for pretty-printing Facet types
This is just a pretty printer, but an imaginative mind could come up with...
- A fully inspectable program state, through a browser interface?
- A modern debugger, exposing all the standard traits and then some instead of a bag of pointers?
Use case: (de)serialization
The facet-reflect crate allows reading and writing (constructing,
initializing) any type that implements Facet — this makes it trivial to
write deserializers, see facet-json, facet-yaml, facet-urlencoded, etc.
Say we have this struct:
use Facet;
We can build it fully through reflection:
# use Facet;
#
#
use PokeUninit;
The inner code here is the kind of code you would write in a deserializer, for example.
Here, we cheated because we (the human) knew the structure of Poke, but in a deserializer,
you would match against the Poke variant, you would inspect a StructDef and its Fields, etc.
Use case: parsing CLI arguments
Facet allows arbitrary attributes (WIP) so you can use it for specifying whether a CLI argument should be positional or named, for example:
use Facet;
let args: Args = from_slice;
eprintln!;
)
)
; ; ; ; ;
Use cases: augmenting static analysis/debuggers
By default the Facet derive macro creates and exports
a global static variable {UPPER_CASE_NAME}_SHAPE referencing the
Shape of the derived Facet trait.
Furthermore, Shape and all nested fields are #[repr(C)].
This information can be used by external processes (like debuggers) to access the layout and vtable data.
For example, suppose we have:
static STATIC_TEST_STRUCT: TestStruct = TestStruct ;
By default, printing this in lldb returns the lengthy:
()
()
However, the TestStruct::SHAPE constant is available at TEST_STRUCT_SHAPE:
()
()
And so we can instead build a simple helper function that takes in a pointer
to the object and it's debug fn and prints out the Debug representation:
()& &)
In this case, debug_print_object is needed because the debug function requires a Formatter
which cannot be constructed externally. But for other operations like Eq, you can resolve it
without needing external methods (but with some additional shenanigans to make lldb happy):
()
()) {
}
()))(&)))(& &)
()
Use cases: beyond
This could be extended to allow RPC, there could be an analoguous derive for traits, it could export statics so that binaries may be inspected — shapes would then be available instead of / in conjunction with debug info.
HTTP routing is a form of deserialization.
This is suitable for all the things serde is bad at: binary formats (specialize
for Vec<u8> without a serde_bytes hack), it could be extended to support formats
like KDL/XML.
I want the derive macros to support arbitrary attributes eventually, which will also
be exposed through Shape.
The types are all non_exhaustive, so there shouldn't be churn in the
ecosystem: crates can do graceful degradation if some types don't implement the
interfaces they expect.
If you have questions or ideas, please open a GitHub issue or discussion — I'm so excited about this.
Ecosystem
The core crates, facet-trait, facet-types etc. are nostd-friendly.
The main facet crate re-exports symbols from:
- facet-core, which defines the main components:
- The
Facettrait and implementations for foreign types (mostlylibstd) - The
Shapestruct along with various vtables and the wholeDeftree - Type-erased pointer helpers like
OpaqueUninit,OpaqueConst, andOpaque - Autoderef specialization trick needed for
facet-derive
- The
- facet-derive, which implements the
Facetderive attribute as a fast/light proc macro powered by unsynn
For struct manipulation and reflection, the following is available:
- facet-reflect, which allows reading from and writing to shapes implementing the
Facettrait. This crate combines the functionality of the formerfacet-peekandfacet-pokecrates, providing a unified interface for reflection and manipulation ofFacettypes.
facet supports deserialization from multiple data formats through dedicated crates:
- facet-json: JSON deserialization
- facet-yaml: YAML deserialization
- facet-msgpack: MessagePack deserialization
- facet-urlencoded: URL-encoded form data deserialization
- facet-args: CLI arguments (a-la clap)
Additionally:
- facet-pretty is able to pretty-print Facet types.
- facet-codegen is internal and generates some of the code of
facet-core
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.