如何为包含函数类型别名的结构实现Debug?

时间:2018-08-26 19:55:49

标签: rust

我有以下类型:

type RangeFn = fn(&Value, &Value) -> bool;

现在我想把它与这个struct放在一起:

#[derive(Debug)]
struct Range {
    fun: RangeFn,
}

但是,如果我有一个以struct作为参数的RangeFn,那么我似乎无法从Debug派生它。如何使RangeFnDebug特性兼容?

1 个答案:

答案 0 :(得分:6)

You can't implement (or derive) a trait you don't own on a type you don't own.

但是,这不是您想要的。您想要的是为// Scroll.js (https://css-tricks.com/snippets/jquery/smooth- scrolling/#comment-1635851) /* * Scroll.js: * 1. added -100 after the .top property which reflects the navigation height. */ $(document).ready(function(){ // Add smooth scrolling to all links $('a').on('click', function(e) { // Make sure this.hash has a value before overriding default behavior if (this.hash !== "") { // Prevent default anchor click behavior e.preventDefault(); // Store hash var hash = this.hash; // Using jQuery's animate() method to add smooth page scroll // The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area $('html, body').animate({ scrollTop: $(hash).offset().top-100 // /* [1] */ }, 800, function(){ // Add hash (#) to URL when done scrolling (default click behavior) window.location.hash = hash; }); } // End if }); 实现Debug,但是您不能通过派生来实现,因为Range没有实现fn。 确实,派生Debug还要求所有字段也都是Debug。然后,您将不得不自己实施Debug;毕竟,这只是正常现象:

Debug

link to playground