Giter VIP home page Giter VIP logo

Comments (4)

IGI-111 avatar IGI-111 commented on May 27, 2024 1

Rust is right here, the signature of a trait method implementation should be the same as the trait method including the constraints. So this is exploiting a bug.

Looks like we didn't include those in the signature check when we implemented trait constraints. But we should.

from sway.

xunilrj avatar xunilrj commented on May 27, 2024

I don't know if this is the solution for this problem, but for my PR I solved this with: be3cc06#diff-35d4b64f583b6ae57aa8095e3d3049788690e178702af2aaf6443030818b74c8R707

from sway.

tritao avatar tritao commented on May 27, 2024

So this seems to be a problem with trait constraints and our replace decls machinery. Me and @esdrubal been looking at this and found a workaround, if you move the where clauses to the method then it seems to work fine.

script;

trait AbiEncode2 {
    fn abi_encode2(self, ref mut buffer: Buffer);
}

impl AbiEncode2 for u64 { fn abi_encode2(self, ref mut buffer: Buffer) { } }
impl AbiEncode2 for u32 { fn abi_encode2(self, ref mut buffer: Buffer) { } }
impl AbiEncode2 for u16 { fn abi_encode2(self, ref mut buffer: Buffer) { } }
impl AbiEncode2 for u8 { fn abi_encode2(self, ref mut buffer: Buffer) { } }

struct GenericBimbam<U> {
    val: U,
}

impl<U> AbiEncode2 for GenericBimbam<U>
 {
    fn abi_encode2(self, ref mut buffer: Buffer) where U: AbiEncode2 {
        self.val.abi_encode2(buffer);
    }
}

struct GenericSnack<T, V> {
    twix: GenericBimbam<T>,
    mars: V,
}

impl<T, V> AbiEncode2 for GenericSnack<T, V>
 {
    fn abi_encode2(self, ref mut buffer: Buffer) where T: AbiEncode2, V: AbiEncode2 {
        self.twix.abi_encode2(buffer);
        self.mars.abi_encode2(buffer);
    }
}

fn encode2<T>(item: T) -> raw_slice where T: AbiEncode2  {
    let mut buffer = Buffer::new();
    item.abi_encode2(buffer);
    buffer.as_raw_slice()
}

fn main() {
    encode2(GenericSnack { twix: GenericBimbam { val: 0u64 }, mars: 2u32 });
}

The proper bugfix for this will probably take a bit of time to get done, so hopefully this is a workable workaround so we can get this feature out ASAP.

from sway.

xunilrj avatar xunilrj commented on May 27, 2024

But should this supported? Rust correctly complains that the impl has more constraints than the trait declaration

error[E0276]: impl has stricter requirements than trait
  --> src/main.rs:36:57
   |
10 |     fn abi_encode2(self, buffer: &mut Buffer);
   |     ------------------------------------------ definition of `abi_encode2` from trait
...
36 |     fn abi_encode2(self,  buffer: &mut Buffer) where T: AbiEncode2, V: AbiEncode2 {
   |                                                         ^^^^^^^^^^ impl has extra requirement `T: AbiEncode2`

from sway.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.