我想构建一个检测对象更改的代理:
代码示例 1 -defineProperty
const me = {
name: "Matt"
}
const proxy = new Proxy(me, {
defineProperty: function(target, key, descriptor) {
console.log(`Property ${key} defined.`);
return Object.defineProperty(target, key, descriptor);
}
});
proxy // { name: 'Matt' }
proxy.name = "Mark";
// Property name defined.
// Mark
proxy.age = 20;
// Property age defined.
// 20
Run Code Online (Sandbox Code Playgroud)
代码示例 1 - 观察
proxy有一个属性name,这是我所期望的。name属性告诉我name已定义;不是我所期望的。age属性告诉我age已经定义;正如我所料。代码示例 2 - …
With terraform 0.12, there is a templatefile function but I haven't figured out the syntax for passing it a non-trivial map as the second argument and using the result to be executed remotely as the newly created instance's provisioning step.
Here's the gist of what I'm trying to do, although it doesn't parse properly because one can't just create a local variable within the resource block named scriptstr.
While I'm really trying to get the output of the templatefile call …
对于一个类型
pub struct Child<'a> {
buf: &'a mut [u8],
}
Run Code Online (Sandbox Code Playgroud)
我可以定义一个特征并实现该类型的特征,但其生命周期绑定到调用函数的上下文(而不是本地循环上下文):
pub trait MakeMut<'a> {
fn make_mut(buf: &'a mut [u8]) -> Self;
}
impl<'a> MakeMut<'a> for Child<'a> {
fn make_mut(buf: &'a mut [u8]) -> Self {
Self { buf }
}
}
Run Code Online (Sandbox Code Playgroud)
首先展示一个有点有效的示例,因为x仅在循环上下文中借用,因为 Child::make_mut 是在函数中硬编码的map1:
pub fn map1<F>(mut func: F)
where
F: FnMut(&mut Child),
{
let mut vec = vec![0; 16];
let x = &mut vec;
for i in 0..2 {
let offset = i * …Run Code Online (Sandbox Code Playgroud)