Skip to content

ptr::swap docs needs clarfication #44479

Closed
@Gankra

Description

@Gankra

The docs state that overlap is permitted, which is intended to cover the lhs==rhs case, but don't specify what that means for partial overlap. For instance:

let mut x = [0u32, 1, 2]; 
let a = &mut x[0] as *mut _ as *mut[u32; 2]; 
let b = &mut x[1] as *mut _ as *mut[u32; 2]; 
ptr::swap(a, b);

// Today: [1, 0, 1]
println!("{:?}", x); 

Reasonable implementations are forced to produce either 0 or 2 for the middle value. We should definitely at least spec that that happens. If we want to be Extra Helpful, we can specify that 0 is used, because there's basically no reason to change this behaviour, or for a different impl to diverge.

Something like "if the LHS and RHS overlap, then the overlapping region of memory will be derived from the LHS", along with the above example to clarify.

A nice thing about specifying it is that, because it's otherwise symmetric, you can swap your lhs/rhs to choose exactly which half you want to "win".

Metadata

Metadata

Assignees

Labels

A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsC-enhancementCategory: An issue proposing an enhancement or a PR with one.P-mediumMedium priority

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions