Mongoose Subdocuments in Nest.js

I dug into the source code and learned how Schema class is converted by the SchemaFactory.createForClass method. Well so how it works? 1. Take a look at this example below: @Schema() export class Cat extends Document { @Prop() name: string; } export const catSchema = SchemaFactory.createForClass(Cat); Basically, when you do SchemaFactory.createForClass(Cat) Nest will convert the … Read more

How to convert Option to Option in the most idiomatic way in Rust?

Option comes with utility methods for various transformations, which are listed in its documentation. For any T that implements Clone (which String does), Option<&T>::cloned does what you’re looking for. Clone is more specific than ToOwned, so .cloned() isn’t an exact match for .map(|x| x.to_owned()). For example, it won’t turn an Option<&str> into an Option<String>; for … Read more

How to avoid writing duplicate accessor functions for mutable and immutable references in Rust?

(playground links to solutions using type parameters and associated types) In this case &T and &mut T are just two different types. Code that is generic over different types (at both compile-time and run-time) is idiomatically written in Rust using traits. For example, given: struct Foo { value: i32 } struct Bar { foo: Foo … Read more

tech