I have a set of structs that are nested, and all the data is optional. Structs are similar to the following (for simplicity I've removed some of the Optionals):
#[derive(Debug, Serialize, Deserialize)]
pub struct Device {
#[serde(rename = "_id")]
pub id: Option<bson::oid::ObjectId>,
pub system: Option<System>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct System {
pub id: Option<u32>,
pub mac: Option<String>,
}
When I query the db, I get the data:
Ok(Some(Device { id: Some(ObjectId("...")), system: Some(System { id: Some(123), mac: Some("1234") })}))
I am trying to access the id value within the System struct. I can access it easily with a few nested match statements, but I was wondering if there is an easier solution. I've been looking at unwrap_or, unwrap_or_else, but I haven't been able to get the syntax correct. Ideally returning a 0 will suffice, as that value is used to denote an error within the application structure. The nested match statements work fine - it is just a bit verbose.
Alternatively I could also just use the aggregation pipeline to get the value. I'm just curious about alternatives.
Thanks.
You can use and_then to apply a function to the contained Ok value, and leave Nones unchanged. For example, imagine the following structure:
struct Foo {
bar: Option<Bar>
}
struct Bar {
baz: Option<Baz>
}
struct Baz {
value: Option<i32>
}
You can then query the nested data like:
fn read_value(foo: Foo) -> Option<i32> {
foo.bar
.and_then(|bar| bar.baz)
.and_then(|baz| baz.value)
}
If any of the Option's are None, the whole expression will be None, otherwise it will be the contained Some(value)
In Swift, at runtime, is it possible to know the name of the module code is running in?
I'd want something like (and this is totally imaginary code)
let moduleName: String = CompileTimeInfo.moduleName
Related.
You might take advantage of the fact that the module name is used as a namespace and debugPrint ing a type will be prefixed with the module name:
enum Test {}
var string: String = ""
debugPrint(Test.self, to: &string)
print("Module name: \(string.split(separator: ".").first ?? "")")
Note: the type must be defined in the actual module. So, wrap first three lines into a function and return the module name, done.
Riffing off of #CouchDeveloper's excellent answer, you can get the module name for an arbitrary Swift type. You can use this to get the module name of any arbitrary code, by creating a type for this purpose.
func moduleName(for type: Any.Type) -> String {
// parse module name from string that looks like "ModuleName.ClassName"
if let subSequence = String(reflecting: type.self).split(separator: ".").first {
return String(subSequence)
} else {
return ""
}
}
print(moduleName(for: String.self)) // -> Swift
enum Test {}
print(moduleName(for: Test.self)) // -> SwiftModuleNameExample
This can even be embedded into a protocol.
public protocol Module {}
extension Module {
static var name: String { moduleName(for: Self.self) }
}
class ThisModule: Module {}
print(ThisModule.name) // -> SwiftModuleNameExample
A macOS command-line Xcode project for this code lives here.
Serde supports applying custom attributes that are used with #[derive(Serialize)]:
#[derive(Serialize)]
struct Resource {
// Always serialized.
name: String,
// Never serialized.
#[serde(skip_serializing)]
hash: String,
// Use a method to decide whether the field should be skipped.
#[serde(skip_serializing_if = "Map::is_empty")]
metadata: Map<String, String>,
}
I understand how to implement a procedural macro (Serialize in this example) but what should I do to implement #[serde(skip_serializing)]? I was unable to find this information anywhere. The docs don't even mention this. I have tried to look at the serde-derive source code but it is very complicated for me.
First you must register all of your attributes in the same place you register your procedural macro. Let's say we want to add two attributes (we still don't talk what will they belong to: structs or fields or both of them):
#[proc_macro_derive(FiniteStateMachine, attributes(state_transitions, state_change))]
pub fn fxsm(input: TokenStream) -> TokenStream {
// ...
}
After that you may already compile your user code with the following:
#[derive(Copy, Clone, Debug, FiniteStateMachine)]
#[state_change(GameEvent, change_condition)] // optional
enum GameState {
#[state_transitions(NeedServer, Ready)]
Prepare { players: u8 },
#[state_transitions(Prepare, Ready)]
NeedServer,
#[state_transitions(Prepare)]
Ready,
}
Without that compiler will give a error with message like:
state_change does not belong to any known attribute.
These attributes are optional and all we have done is allow them to be to specified. When you derive your procedural macro you may check for everything you want (including attributes existence) and panic! on some condition with meaningful message which will be told by the compiler.
Now we will talk about handling the attribute! Let's forget about state_transitions attribute because it's handling will not vary too much from handling struct/enum attributes (actually it is only a little bit more code) and talk about state_change. The syn crate gives you all the needed information about definitions (but not implementations unfortunately (I am talking about impl here) but this is enough for handling attributes of course). To be more detailed, we need syn::DeriveInput, syn::Body, syn::Variant, syn::Attribute and finally syn::MetaItem.
To handle the attribute of a field you need to go through all these structures from one to another. When you reach Vec<syn:: Attribute> - this is what you want, a list of all attributes of a field. Here our state_transitions can be found. When you find it, you may want to get its content and this can be done by using matching syn::MetaItem enum. Just read the docs :) Here is a simple example code which panics when we find state_change attribute on some field plus it checks does our target entity derive Copy or Clone or neither of them:
#[proc_macro_derive(FiniteStateMachine, attributes(state_transitions, state_change))]
pub fn fxsm(input: TokenStream) -> TokenStream {
// Construct a string representation of the type definition
let s = input.to_string();
// Parse the string representation
let ast = syn::parse_derive_input(&s).unwrap();
// Build the impl
let gen = impl_fsm(&ast);
// Return the generated impl
gen.parse().unwrap()
}
fn impl_fsm(ast: &syn::DeriveInput) -> Tokens {
const STATE_CHANGE_ATTR_NAME: &'static str = "state_change";
if let syn::Body::Enum(ref variants) = ast.body {
// Looks for state_change attriute (our attribute)
if let Some(ref a) = ast.attrs.iter().find(|a| a.name() == STATE_CHANGE_ATTR_NAME) {
if let syn::MetaItem::List(_, ref nested) = a.value {
panic!("Found our attribute with contents: {:?}", nested);
}
}
// Looks for derive impls (not our attribute)
if let Some(ref a) = ast.attrs.iter().find(|a| a.name() == "derive") {
if let syn::MetaItem::List(_, ref nested) = a.value {
if derives(nested, "Copy") {
return gen_for_copyable(&ast.ident, &variants, &ast.generics);
} else if derives(nested, "Clone") {
return gen_for_clonable(&ast.ident, &variants, &ast.generics);
} else {
panic!("Unable to produce Finite State Machine code on a enum which does not drive Copy nor Clone traits.");
}
} else {
panic!("Unable to produce Finite State Machine code on a enum which does not drive Copy nor Clone traits.");
}
} else {
panic!("How have you been able to call me without derive!?!?");
}
} else {
panic!("Finite State Machine must be derived on a enum.");
}
}
fn derives(nested: &[syn::NestedMetaItem], trait_name: &str) -> bool {
nested.iter().find(|n| {
if let syn::NestedMetaItem::MetaItem(ref mt) = **n {
if let syn::MetaItem::Word(ref id) = *mt {
return id == trait_name;
}
return false
}
false
}).is_some()
}
You may be interested in reading serde_codegen_internals, serde_derive, serenity's #[command] attr, another small project of mine - unique-type-id, fxsm-derive. The last link is actually my own project to explain to myself how to use procedural macros in Rust.
After some Rust 1.15 and updating the syn crate, it is no longer possible to check derives of a enums/structs, however, everything else works okay.
You implement attributes on fields as part of the derive macro for the struct (you can only implement derive macros for structs and enums).
Serde does this by checking every field for an attribute within the structures provided by syn and changing the code generation accordingly.
You can find the relevant code here: https://github.com/serde-rs/serde/blob/master/serde_derive/src/internals/attr.rs
To expand Victor Polevoy's answer when it comes to the state_transitions attribute. I'm providing an example of how to extract the field attribute #[state_transitions(NeedServer, Ready)] on a enum that derives #[derive(FiniteStateMachine)]:
#[derive(FiniteStateMachine)]
enum GameState {
#[state_transitions(NeedServer, Ready)] // <-- extract this
Prepare { players: u8 },
#[state_transitions(Prepare, Ready)]
NeedServer,
#[state_transitions(Prepare)]
Ready,
}
use proc_macro::TokenStream;
#[proc_macro_derive(FiniteStateMachine, attributes(state_transitions))]
pub fn finite_state_machine(input: TokenStream) -> TokenStream {
let ast = syn::parse(input).unwrap();
// Extract the enum variants
let variants: Vec<&syn::Variant> = match &ast.data {
syn::Data::Enum(ref data_enum) => data_enum.variants.iter().collect(),
other => panic!("#[derive(FiniteStateMachine)] expects enum, got {:#?}", other)
};
// For each variant, extract the attributes
let _ = variants.iter().map(|variant| {
let attrs = variant.attrs.iter()
// checks attribute named "state_transitions(...)"
.find_map(|attr| match attr.path.is_ident("state_transitions") {
true => Some(&attr.tokens),
false => None,
})
.expect("#[derive(FiniteStateMachine)] expects attribute macros #[state_transitions(...)] on each variant, found none");
// outputs: attr: "(NeedServer, Ready)"
eprintln!("attr: {:#?}", attrs.to_string());
// do something with the extracted attributes
...
})
.collect();
...
}
The content of the extracted attrs (typed TokenStream) looks like this:
TokenStream [
Group {
delimiter: Parenthesis,
stream: TokenStream [
Ident {
ident: "NeedServer",
span: #0 bytes(5511..5521),
},
Punct {
ch: ',',
spacing: Alone,
span: #0 bytes(5521..5522),
},
Ident {
ident: "Ready",
span: #0 bytes(5523..5528),
},
],
span: #0 bytes(5510..5529),
},
]
I want to write a macro like this:
macro_rules! a {
( $n:ident, $t:ty ) => {
struct $n {
x: $t
}
}
}
But $t should implement Add, Sub and Mul traits. How can I check it at compile-time?
First, solve the problem without macros. One solution is to create undocumented private functions that will fail compilation if your conditions aren't met:
struct MyType {
age: i32,
name: String,
}
const _: () = {
fn assert_send<T: Send>() {}
fn assert_sync<T: Sync>() {}
// RFC 2056
fn assert_all() {
assert_send::<MyType>();
assert_sync::<MyType>();
}
};
Then, modify the simple solution to use macros:
macro_rules! example {
($name:ident, $field:ty) => {
struct $name {
x: $field,
}
const _: () = {
fn assert_add<T: std::ops::Add<$field, Output = $field>>() {}
fn assert_mul<T: std::ops::Mul<$field, Output = $field>>() {}
// RFC 2056
fn assert_all() {
assert_add::<$field>();
assert_mul::<$field>();
}
};
};
}
example!(Moo, u8);
example!(Woof, bool);
In both cases, we create a dummy const value to scope the functions and their calls, avoiding name clashes.
I would then trust in the optimizer to remove the code at compile time, so I wouldn't expect any additional bloat.
Major thanks to Chris Morgan for providing a better version of this that supports non-object-safe traits.
It's worth highlighting RFC 2056 which will allow for "trivial" constraints in where clauses. Once implemented, clauses like this would be accepted:
impl Foo for Bar
where
i32: Iterator,
{}
This exact behavior has changed multiple times during Rust's history and RFC 2056 pins it down. To keep the behavior we want in this case, we need to call the assertion functions from another function which has no constraints (and thus must always be true).
I'm writing a Rust wrapper for a binary protocol (Netlink) and for that purpose I need to encode some data (Strings and different sorts of ints and uints) into some kind of buffer, and I saw that Vec<u8> is uausally used for that. My current pseudocode looks like this:
struct NetlinkHeader {
length: u32,
_type: u16,
flags: u16,
sequence: u32,
pid: u32
}
impl NetlinkSerializable for NetlinkHeader {
fn to_wire_format(&self) -> Result<str, &'static str> {
let mut binary_data: Vec<u8> = Vec::with_capacity(16);
binary_data.add_binary_representation(self.length);
binary_data.add_binary_representation(self._type);
binary_data.add_binary_representation(self.flags);
binary_data.add_binary_representation(self.sequence);
binary_data.add_binary_representation(self.pid);
}
}
I need help implementing something that does what add_binary_representation in the above pseudocode would do.