Skip to content

Remove Deref and DerefMut for Json #2909

Closed
@bobozaur

Description

@bobozaur

Bug Description

Perhaps not a bug, but a questionable implementation decision.

The Json newtype wrapper implements Deref and DerefMut along with the more correct AsRef and AsMut traits. I'd personally say that Deref and DerefMut should be removed, perhaps in 0.8.0 as it would be a breaking change.

The rationale is that the impls are arguably redundant and even lead to confusion in some occasions for the sole benefit of a shortcut in field access (which ends up giving references instead of owned types as with regular structs field access).

Minimal Reproduction

#[derive(Clone)]
struct MyStruct {
    field: String
}

let s = MyStruct {field: "something".to_owned()};
let x = Json(s.clone());

let _ = s.field; // has type String
let _ = x.field; // has type &String - confusing
let _ = x.0.field // has type String
let _ = x.as_ref().field // has type &String - intuitive due to the as_ref() call

Info

  • SQLx version: 0.7.1, most likely older
  • SQLx features enabled: None
  • Database server and version: N/A
  • Operating system: N/A
  • rustc --version: N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions