Closed
Description
I wonder if there is any use for ptr::addr_of()
to return address of a temporary copied version of its argument, but it seems a bit annoying. E.g. for following code, different address will be printed.
fn main() {
let foo = 1;
log(error, addr_of(foo));
log(error, addr_of(foo));
}
Use following version of addr_of
instead, the address will be consistent.
fn addr_of<T>(&val: T) -> *T { ret ptr::addr_of(val); }
And a real world example:
fn get_elem(mixer: snd_mixer, name: str) -> snd_elem {
let sid = null();
asound::snd_mixer_selem_id_malloc(addr_of(sid));
asound::snd_mixer_selem_id_set_index(sid, 0);
as_c_str(name) {|buf|
asound::snd_mixer_selem_id_set_name(sid, buf);
}
ret asound::snd_mixer_find_selem(mixer, sid);
}
asound::snd_mixer_selem_id_malloc()
will allocate a buffer and assign the address to sid
。But as sid
is implicit copied, sid
remains 0
after this function call.
Can ptr::addr_of()
be defined as always pass by reference to avoid implicit copy?