<p dir="auto">I'm fleshing out the json library to add support for a <code class="notranslate">to_json</code> iface, among other things, and I'm finding a lot of code duplication between my instances. Here's an example:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="iface to_json {
    fn to_json_writer(wr: io::writer);
    fn to_json_str() -&gt; str;
}

impl of to_json for float {
    fn to_json_writer(wr: io::writer) {
        wr.write_str(float::to_str(self, 6u));
    }

    fn to_json_str() -&gt; str {
        io::with_str_writer(self.to_json_writer)
    }
}

impl of to_json for int {
    fn to_json_writer(wr: io::writer) {
        wr.write_str(int::str(self));
    }

    fn to_json_str() -&gt; str {
        io::with_str_writer(self.to_json_writer)
    }
}
..."><pre class="notranslate"><code class="notranslate">iface to_json {
    fn to_json_writer(wr: io::writer);
    fn to_json_str() -&gt; str;
}

impl of to_json for float {
    fn to_json_writer(wr: io::writer) {
        wr.write_str(float::to_str(self, 6u));
    }

    fn to_json_str() -&gt; str {
        io::with_str_writer(self.to_json_writer)
    }
}

impl of to_json for int {
    fn to_json_writer(wr: io::writer) {
        wr.write_str(int::str(self));
    }

    fn to_json_str() -&gt; str {
        io::with_str_writer(self.to_json_writer)
    }
}
...
</code></pre></div>
<p dir="auto">Every implementation's <code class="notranslate">to_json_str</code> is just a straight copy of the others. It would be much nicer if we could provide default versions of functions like <code class="notranslate">to_json_str</code> and just have the implementations inherit it.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="iface to_json {
    fn to_json_writer(wr: io::writer);
    fn to_json_str() -&gt; str {
        io::with_str_writer(self.to_json_writer)
    }
}"><pre class="notranslate"><code class="notranslate">iface to_json {
    fn to_json_writer(wr: io::writer);
    fn to_json_str() -&gt; str {
        io::with_str_writer(self.to_json_writer)
    }
}
</code></pre></div>
<p dir="auto">Hi folks,</p>
<p dir="auto">I think my code is wrong, but since it says this is a compiler bug I thought I'd file it.</p>
<p dir="auto">I'm on <code class="notranslate">rustc 0.11.0-pre-nightly (bb5695b95c288c442dbe528f7e1c1b08f79f033d 2014-06-29 23:36:43 +0000)</code> (os x)</p>
<p dir="auto">I have the following code:</p>
<div class="highlight highlight-source-rust notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="extern crate crypto = &quot;rust-crypto&quot;;

use std::io::stdio::println;
use crypto::md5::Md5;
use crypto::digest::Digest;

fn main() {
  println!(&quot;{}&quot;, ketama(&quot;Hello World!&quot;));
}

fn ketama(key: &amp;str) -&gt; u8 {

    // 2: Create MD5 digset
    let mut sh = Md5::new();
    sh.input_str(key);
    let mut digest = Vec::from_elem((sh.output_bits()+7)/8, 0u8);
    sh.result(digest.as_mut_slice());

    // 3: Construct the ketama hash
    let result = 
        ((digest.get(3) &amp; 0xFF) &lt;&lt; 24) |
        ((digest.get(2) &amp; 0xFF) &lt;&lt; 16) |
        ((digest.get(1) &amp; 0xFF) &lt;&lt; 8)  |
        (digest.get(0) &amp; 0xFF);

    result
}

#[cfg(test)]
mod tests {

    use ketama;

    fn test_ketama() {
        assert!(3979113294, ketama(&quot;26&quot;));
    }

}"><pre class="notranslate"><span class="pl-k">extern</span> <span class="pl-k">crate</span> crypto = <span class="pl-s">"rust-crypto"</span><span class="pl-kos">;</span>

<span class="pl-k">use</span> std<span class="pl-kos">::</span>io<span class="pl-kos">::</span>stdio<span class="pl-kos">::</span>println<span class="pl-kos">;</span>
<span class="pl-k">use</span> crypto<span class="pl-kos">::</span>md5<span class="pl-kos">::</span><span class="pl-v">Md5</span><span class="pl-kos">;</span>
<span class="pl-k">use</span> crypto<span class="pl-kos">::</span>digest<span class="pl-kos">::</span><span class="pl-v">Digest</span><span class="pl-kos">;</span>

<span class="pl-k">fn</span> <span class="pl-en">main</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
  <span class="pl-en">println</span><span class="pl-en">!</span><span class="pl-kos">(</span><span class="pl-s">"{}"</span>, ketama<span class="pl-kos">(</span><span class="pl-s">"Hello World!"</span><span class="pl-kos">)</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span>

<span class="pl-k">fn</span> <span class="pl-en">ketama</span><span class="pl-kos">(</span><span class="pl-s1">key</span><span class="pl-kos">:</span> <span class="pl-c1">&amp;</span><span class="pl-smi">str</span><span class="pl-kos">)</span> -&gt; <span class="pl-smi">u8</span> <span class="pl-kos">{</span>

    <span class="pl-c">// 2: Create MD5 digset</span>
    <span class="pl-k">let</span> <span class="pl-k">mut</span> sh = <span class="pl-smi">Md5</span><span class="pl-kos">::</span><span class="pl-en">new</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    sh<span class="pl-kos">.</span><span class="pl-en">input_str</span><span class="pl-kos">(</span>key<span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-k">let</span> <span class="pl-k">mut</span> digest = <span class="pl-smi">Vec</span><span class="pl-kos">::</span><span class="pl-en">from_elem</span><span class="pl-kos">(</span><span class="pl-kos">(</span>sh<span class="pl-kos">.</span><span class="pl-en">output_bits</span><span class="pl-kos">(</span><span class="pl-kos">)</span>+<span class="pl-c1">7</span><span class="pl-kos">)</span>/<span class="pl-c1">8</span><span class="pl-kos">,</span> <span class="pl-c1">0u8</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    sh<span class="pl-kos">.</span><span class="pl-en">result</span><span class="pl-kos">(</span>digest<span class="pl-kos">.</span><span class="pl-en">as_mut_slice</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

    <span class="pl-c">// 3: Construct the ketama hash</span>
    <span class="pl-k">let</span> result = 
        <span class="pl-kos">(</span><span class="pl-kos">(</span>digest<span class="pl-kos">.</span><span class="pl-en">get</span><span class="pl-kos">(</span><span class="pl-c1">3</span><span class="pl-kos">)</span> <span class="pl-c1">&amp;</span> <span class="pl-c1">0xFF</span><span class="pl-kos">)</span> &lt;&lt; <span class="pl-c1">24</span><span class="pl-kos">)</span> |
        <span class="pl-kos">(</span><span class="pl-kos">(</span>digest<span class="pl-kos">.</span><span class="pl-en">get</span><span class="pl-kos">(</span><span class="pl-c1">2</span><span class="pl-kos">)</span> <span class="pl-c1">&amp;</span> <span class="pl-c1">0xFF</span><span class="pl-kos">)</span> &lt;&lt; <span class="pl-c1">16</span><span class="pl-kos">)</span> |
        <span class="pl-kos">(</span><span class="pl-kos">(</span>digest<span class="pl-kos">.</span><span class="pl-en">get</span><span class="pl-kos">(</span><span class="pl-c1">1</span><span class="pl-kos">)</span> <span class="pl-c1">&amp;</span> <span class="pl-c1">0xFF</span><span class="pl-kos">)</span> &lt;&lt; <span class="pl-c1">8</span><span class="pl-kos">)</span>  |
        <span class="pl-kos">(</span>digest<span class="pl-kos">.</span><span class="pl-en">get</span><span class="pl-kos">(</span><span class="pl-c1">0</span><span class="pl-kos">)</span> <span class="pl-c1">&amp;</span> <span class="pl-c1">0xFF</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

    result
<span class="pl-kos">}</span>

<span class="pl-c1">#<span class="pl-kos">[</span>cfg<span class="pl-kos">(</span>test<span class="pl-kos">)</span><span class="pl-kos">]</span></span>
<span class="pl-k">mod</span> tests <span class="pl-kos">{</span>

    <span class="pl-k">use</span> ketama<span class="pl-kos">;</span>

    <span class="pl-k">fn</span> <span class="pl-en">test_ketama</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
        <span class="pl-en">assert</span><span class="pl-en">!</span><span class="pl-kos">(</span><span class="pl-c1">3979113294</span>, ketama<span class="pl-kos">(</span><span class="pl-s">"26"</span><span class="pl-kos">)</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-kos">}</span>

<span class="pl-kos">}</span></pre></div>
<p dir="auto">and the error popped up as I added the <code class="notranslate">use ketama</code> to the tests module.</p>
<p dir="auto">When I run <code class="notranslate">cargo test</code> on my file I get:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="┌─[✗]─[michael@daschlbase]─[~/code/rust/ketama]
└──╼ RUST_BACKTRACE=1 cargo test
       Fresh rust-crypto v0.1.0 (https://github.com/DaGenix/rust-crypto.git)
   Compiling ketama-rs v0.1.0 (file:/Users/michaelnitschinger/code/rust/ketama)
&lt;std macros&gt;:8:12: 35:21 error: mismatched types: expected `bool` but found `&lt;generic integer #0&gt;` (expected bool but found integral variable)
&lt;std macros&gt;:8         if !$cond {
&lt;std macros&gt;:9             fail!($($arg),+)
&lt;std macros&gt;:10         }
&lt;std macros&gt;:11     );
&lt;std macros&gt;:12 )
error: internal compiler error: unexpected failure
note: the compiler hit an unexpected failure path. this is a bug.
note: we would appreciate a bug report: http://doc.rust-lang.org/complement-bugreport.html
note: run with `RUST_BACKTRACE=1` for a backtrace
task 'rustc' failed at 'index out of bounds: the len is 12 but the index is 12', /Users/rustbuild/src/rust-buildbot/slave/nightly-mac/build/src/libsyntax/lib.rs:1

stack backtrace:
   1:        0x1054f0655 - rt::backtrace::imp::write::h33d600b14bcc0ad7tPp::v0.11.0.pre
   2:        0x1054f6bf7 - failure::on_fail::h8275f76a6db57202M5p::v0.11.0.pre
   3:        0x1057a7e09 - unwind::begin_unwind_inner::hd390bdc7271bbf4cIVd::v0.11.0.pre
   4:        0x1057a78bd - unwind::begin_unwind_fmt::hfa8fc7b11f5dad16bTd::v0.11.0.pre
   5:        0x1057a7711 - rust_begin_unwind
   6:        0x1057f3bbe - failure::begin_unwind::h29121687d16d4062X2v::v0.11.0.pre
   7:        0x1057f8189 - failure::fail_bounds_check::hb0b1b63e7b73cbf690v::v0.11.0.pre
   8:        0x104c7715d - codemap::FileMap::get_line::hf69850111b874f5dRDd::v0.11.0.pre
   9:        0x104c73656 - diagnostic::emit::hc4f6ec047e6b64740uc::v0.11.0.pre
  10:        0x104c6e652 - diagnostic::EmitterWriter.Emitter::emit::h892977fb7f421d339qc::v0.11.0.pre
  11:        0x104c6c953 - diagnostic::Handler::emit::h755ef325bde394dakec::v0.11.0.pre
  12:        0x104c6b975 - diagnostic::SpanHandler::span_err::h266f98379c75b6f9Q4b::v0.11.0.pre
  13:        0x102669cb6 - middle::typeck::infer::InferCtxt&lt;'a&gt;::type_error_message_str_with_expected::h506fef97717d30f26Kw::v0.11.0.pre
  14:        0x102714993 - middle::typeck::infer::InferCtxt&lt;'a&gt;::type_error_message::h4ac10c0197867eadOSw::v0.11.0.pre
  15:        0x102714ad9 - middle::typeck::infer::InferCtxt&lt;'a&gt;::report_mismatched_types::h09f90236d1ff10f5TTw::v0.11.0.pre
  16:        0x1026cf748 - middle::typeck::check::demand::suptype::closure.91708
  17:        0x1026cf680 - middle::typeck::check::demand::suptype_with_fn::h5aef443dd5c41e99dga::v0.11.0.pre
  18:        0x10271eec8 - middle::typeck::check::check_expr_has_type::closure.92600
  19:        0x102718eec - middle::typeck::check::check_expr_with_unifier::h7a4a57fad133af797rf::v0.11.0.pre
  20:        0x1027198ef - middle::typeck::check::check_expr_with_unifier::h7a4a57fad133af797rf::v0.11.0.pre
  21:        0x102733001 - middle::typeck::check::check_stmt::h2a033ee04cabfc221ih::v0.11.0.pre
  22:        0x1026ef142 - middle::typeck::check::check_block_with_expected::h020999ba5c44c098cnh::v0.11.0.pre
  23:        0x1026ead0d - middle::typeck::check::check_fn::h6413530500ad7359jUc::v0.11.0.pre
  24:        0x1026ea6e0 - middle::typeck::check::check_bare_fn::h42022f9acab71088RJc::v0.11.0.pre
  25:        0x1026e43ee - middle::typeck::check::check_item::h06e560567b39b12e5gd::v0.11.0.pre
  26:        0x1026e5d3d - visit::walk_item::h12564307020263923332::v0.11.0.pre
  27:        0x1026ea4c0 - middle::typeck::check::check_item_types::h8b297d641b758de99Ic::v0.11.0.pre
  28:        0x10285c24e - middle::typeck::check_crate::h901cb82af492c67cSJC::v0.11.0.pre
  29:        0x102cb0dc8 - driver::driver::phase_3_run_analysis_passes::h5dd5f16878eed1ed84p::v0.11.0.pre
  30:        0x102cabe15 - driver::driver::compile_input::h85050a3ac86049856Rp::v0.11.0.pre
  31:        0x102d7256b - driver::run_compiler::hae8f05bffa41c7b05Bs::v0.11.0.pre
  32:        0x102d6fd86 - driver::main_args::closure.117272
  33:        0x102d8445b - task::TaskBuilder&lt;S&gt;::try_future::closure.118413
  34:        0x102d842f4 - task::TaskBuilder&lt;S&gt;::spawn_internal::closure.118390
  35:        0x104be895c - task::spawn_opts::closure.7562
  36:        0x10580d0ac - rust_try
  37:        0x1057a42aa - unwind::try::h803831d3cb7669807Jd::v0.11.0.pre
  38:        0x1057a3fac - task::Task::run::h4a0e5408354628dfZVc::v0.11.0.pre
  39:        0x104be87eb - task::spawn_opts::closure.7507
  40:        0x1057a6b19 - thread::thread_start::h5aa600706e7c5c88xid::v0.11.0.pre
  41:     0x7fff92c10899 - _pthread_body
  42:     0x7fff92c1072a - _pthread_struct_init"><pre class="notranslate"><code class="notranslate">┌─[✗]─[michael@daschlbase]─[~/code/rust/ketama]
└──╼ RUST_BACKTRACE=1 cargo test
       Fresh rust-crypto v0.1.0 (https://github.com/DaGenix/rust-crypto.git)
   Compiling ketama-rs v0.1.0 (file:/Users/michaelnitschinger/code/rust/ketama)
&lt;std macros&gt;:8:12: 35:21 error: mismatched types: expected `bool` but found `&lt;generic integer #0&gt;` (expected bool but found integral variable)
&lt;std macros&gt;:8         if !$cond {
&lt;std macros&gt;:9             fail!($($arg),+)
&lt;std macros&gt;:10         }
&lt;std macros&gt;:11     );
&lt;std macros&gt;:12 )
error: internal compiler error: unexpected failure
note: the compiler hit an unexpected failure path. this is a bug.
note: we would appreciate a bug report: http://doc.rust-lang.org/complement-bugreport.html
note: run with `RUST_BACKTRACE=1` for a backtrace
task 'rustc' failed at 'index out of bounds: the len is 12 but the index is 12', /Users/rustbuild/src/rust-buildbot/slave/nightly-mac/build/src/libsyntax/lib.rs:1

stack backtrace:
   1:        0x1054f0655 - rt::backtrace::imp::write::h33d600b14bcc0ad7tPp::v0.11.0.pre
   2:        0x1054f6bf7 - failure::on_fail::h8275f76a6db57202M5p::v0.11.0.pre
   3:        0x1057a7e09 - unwind::begin_unwind_inner::hd390bdc7271bbf4cIVd::v0.11.0.pre
   4:        0x1057a78bd - unwind::begin_unwind_fmt::hfa8fc7b11f5dad16bTd::v0.11.0.pre
   5:        0x1057a7711 - rust_begin_unwind
   6:        0x1057f3bbe - failure::begin_unwind::h29121687d16d4062X2v::v0.11.0.pre
   7:        0x1057f8189 - failure::fail_bounds_check::hb0b1b63e7b73cbf690v::v0.11.0.pre
   8:        0x104c7715d - codemap::FileMap::get_line::hf69850111b874f5dRDd::v0.11.0.pre
   9:        0x104c73656 - diagnostic::emit::hc4f6ec047e6b64740uc::v0.11.0.pre
  10:        0x104c6e652 - diagnostic::EmitterWriter.Emitter::emit::h892977fb7f421d339qc::v0.11.0.pre
  11:        0x104c6c953 - diagnostic::Handler::emit::h755ef325bde394dakec::v0.11.0.pre
  12:        0x104c6b975 - diagnostic::SpanHandler::span_err::h266f98379c75b6f9Q4b::v0.11.0.pre
  13:        0x102669cb6 - middle::typeck::infer::InferCtxt&lt;'a&gt;::type_error_message_str_with_expected::h506fef97717d30f26Kw::v0.11.0.pre
  14:        0x102714993 - middle::typeck::infer::InferCtxt&lt;'a&gt;::type_error_message::h4ac10c0197867eadOSw::v0.11.0.pre
  15:        0x102714ad9 - middle::typeck::infer::InferCtxt&lt;'a&gt;::report_mismatched_types::h09f90236d1ff10f5TTw::v0.11.0.pre
  16:        0x1026cf748 - middle::typeck::check::demand::suptype::closure.91708
  17:        0x1026cf680 - middle::typeck::check::demand::suptype_with_fn::h5aef443dd5c41e99dga::v0.11.0.pre
  18:        0x10271eec8 - middle::typeck::check::check_expr_has_type::closure.92600
  19:        0x102718eec - middle::typeck::check::check_expr_with_unifier::h7a4a57fad133af797rf::v0.11.0.pre
  20:        0x1027198ef - middle::typeck::check::check_expr_with_unifier::h7a4a57fad133af797rf::v0.11.0.pre
  21:        0x102733001 - middle::typeck::check::check_stmt::h2a033ee04cabfc221ih::v0.11.0.pre
  22:        0x1026ef142 - middle::typeck::check::check_block_with_expected::h020999ba5c44c098cnh::v0.11.0.pre
  23:        0x1026ead0d - middle::typeck::check::check_fn::h6413530500ad7359jUc::v0.11.0.pre
  24:        0x1026ea6e0 - middle::typeck::check::check_bare_fn::h42022f9acab71088RJc::v0.11.0.pre
  25:        0x1026e43ee - middle::typeck::check::check_item::h06e560567b39b12e5gd::v0.11.0.pre
  26:        0x1026e5d3d - visit::walk_item::h12564307020263923332::v0.11.0.pre
  27:        0x1026ea4c0 - middle::typeck::check::check_item_types::h8b297d641b758de99Ic::v0.11.0.pre
  28:        0x10285c24e - middle::typeck::check_crate::h901cb82af492c67cSJC::v0.11.0.pre
  29:        0x102cb0dc8 - driver::driver::phase_3_run_analysis_passes::h5dd5f16878eed1ed84p::v0.11.0.pre
  30:        0x102cabe15 - driver::driver::compile_input::h85050a3ac86049856Rp::v0.11.0.pre
  31:        0x102d7256b - driver::run_compiler::hae8f05bffa41c7b05Bs::v0.11.0.pre
  32:        0x102d6fd86 - driver::main_args::closure.117272
  33:        0x102d8445b - task::TaskBuilder&lt;S&gt;::try_future::closure.118413
  34:        0x102d842f4 - task::TaskBuilder&lt;S&gt;::spawn_internal::closure.118390
  35:        0x104be895c - task::spawn_opts::closure.7562
  36:        0x10580d0ac - rust_try
  37:        0x1057a42aa - unwind::try::h803831d3cb7669807Jd::v0.11.0.pre
  38:        0x1057a3fac - task::Task::run::h4a0e5408354628dfZVc::v0.11.0.pre
  39:        0x104be87eb - task::spawn_opts::closure.7507
  40:        0x1057a6b19 - thread::thread_start::h5aa600706e7c5c88xid::v0.11.0.pre
  41:     0x7fff92c10899 - _pthread_body
  42:     0x7fff92c1072a - _pthread_struct_init
</code></pre></div>
<p dir="auto">feel free to close if its not an issue.<br>
Cheers,<br>
Michael</p>