Skip to content

Commit 0048bf6

Browse files
committed
add copy_from in addition to copy_to
1 parent 1aabd25 commit 0048bf6

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

text/0000-unsafe-pointer-reform.md

+12-6
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ impl<T> *(const|mut) T {
103103
unsafe fn read_volatile(self) -> T;
104104
unsafe fn read_unaligned(self) -> T;
105105

106-
// NOTE: name changed from the original static fn to make it
107-
// easier to remember argument order
108106
unsafe fn copy_to(self, dest: *mut T, count: usize);
109107
unsafe fn copy_to_nonoverlapping(self, dest: *mut T, count: usize);
108+
unsafe fn copy_from(self, src: *mut T, count: usize);
109+
unsafe fn copy_from_nonoverlapping(self, src: *mut T, count: usize);
110110
}
111111
```
112112

@@ -125,7 +125,7 @@ impl<T> *mut T {
125125
}
126126
```
127127

128-
128+
(see the alternatives for why we provide both copy_to and copy_from)
129129

130130

131131
## Unsigned Offset
@@ -188,13 +188,19 @@ We could make `offset` generic so it accepts `usize` and `isize`. However we wou
188188

189189

190190

191-
## `copy_from`
191+
## Only one of `copy_to` or `copy_from`
192192

193193
`copy` is the only mutating `ptr` operation that doesn't write to the *first* argument. In fact, it's clearly backwards compared to C's memcpy. Instead it's ordered in analogy to `fs::copy`.
194194

195-
Methodization could be an opportunity to "fix" this, and reorder the arguments. I don't have any strong feelings for which order is better, but I am concerned that doing this reordering will lead to developer errors. Either in translating to the new methods, or in using the method order when deciding to use the old functions.
195+
Methodization could be an opportunity to "fix" this, and reorder the arguments, providing only `copy_from`. However there is concern that this will lead to users doing a blind migration without checking argument order.
196+
197+
One possibly solution would be deprecating `ptr::copy` along with this as a "signal" that something strange has happened. But as discussed in the following section, immediately deprecating an API along with the introduction of its replacement tends to cause a mess in the broader ecosystem.
198+
199+
On the other hand, `copy_to` isn't as idiomatic (see: `clone_from`), and there was disastisfaction in reinforcing this API design quirk.
200+
201+
As a compromise, we opted to provide both, forcing users of `copy` to decided which they want. Ideally this will be copy_from with reversed arguments, as this is more idiomatic. Longterm we can look to deprecating `copy_to` and `ptr::copy` if desirable. Otherwise having these duplicate methods isn't a big deal (and is *technically* a bit more convenient for users with a reference and a raw pointer).
202+
196203

197-
This option should only be considered in tandem with a deprecation of `ptr::copy`. But as discussed in the following section, immediately deprecating an API along with the introduction of its replacement tends to cause a mess in the broader ecosystem.
198204

199205

200206

0 commit comments

Comments
 (0)