{"id":333,"date":"2024-05-13T23:55:20","date_gmt":"2024-05-13T15:55:20","guid":{"rendered":"https:\/\/cococat.top\/?p=333"},"modified":"2024-05-13T23:55:20","modified_gmt":"2024-05-13T15:55:20","slug":"rust-note-2-explicit-trait-object-type","status":"publish","type":"post","link":"https:\/\/cococat.top\/index.php\/2024\/05\/13\/rust-note-2-explicit-trait-object-type\/","title":{"rendered":"rust \u7b14\u8bb02: \u4e00\u4e2a\u7279\u6b8a\u7684\u573a\u666f\u4e4b\u7f16\u8bd1\u5668\u4e0d\u80fd\u63a8\u65ad\u51fa\u7279\u5f81\u5bf9\u8c61\u7684\u7c7b\u578b"},"content":{"rendered":"<h2>1. \u573a\u666f<\/h2>\n<p>\u5f53\u6211\u4eec\u9700\u8981\u7528\u7279\u5f81\u5bf9\u8c61\u7684\u65f6\u5019\uff0c\u5927\u90e8\u5206\u60c5\u51b5\u4e0b Rust \u7f16\u8bd1\u5668\u53ef\u4ee5\u81ea\u52a8\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad\u3002\u4f46\u662f\u5728\u5c11\u6570\u573a\u666f\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u663e\u5f0f\u58f0\u660e\u7279\u5f81\u5bf9\u8c61\uff0c\u5176\u4e2d\u4e00\u4e2a\u4f8b\u5b50\u5c31\u662f\u5f15\u7528 + \u5173\u8054\u7c7b\u578b\u540c\u65f6\u51fa\u73b0\u3002<\/p>\n<p>\u76f4\u63a5\u6765\u770b\u4e00\u4e2a\u4f8b\u5b50\uff0c\u4e3a\u4e86\u66f4\u52a0\u5f62\u8c61\u5730\u89e3\u91ca\uff0c\u6211\u4eec\u7528\u670d\u52a1\u5668\u7aef\u7a0b\u5e8f\u7684\u573a\u666f\u6765\u547d\u540d\u51fa\u73b0\u7684\u7c7b\u578b\u548c\u7279\u5f81\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u4e00\u4e2a <code>Listener<\/code> \u7684 trait\uff1a<\/p>\n<pre><code class=\"language-rust\">trait Listener {\n    type Conn;\n    fn accept(&amp;self) -&gt; Self::Conn;\n}<\/code><\/pre>\n<p><code>Listener<\/code> \u9700\u8981\u6709\u4e00\u4e2a\u5173\u8054\u7c7b\u578b <code>Conn<\/code> \uff0c\u4f5c\u4e3a\u5176 <code>accept()<\/code> \u65b9\u6cd5\u8fd4\u56de\u7684\u5bf9\u8c61\uff0c\u4f8b\u5982\u4e00\u4e2a TCP \u7684 <code>Listener<\/code> \u9700\u8981\u8fd4\u56de\u4e00\u4e2a\u7c7b\u4f3c\u4e8e <code>TcpConnection<\/code> \u7684\u4e1c\u897f\uff0c\u8fd9\u4e9b\u90fd\u662f\u975e\u5e38\u76f4\u89c2\u7684\u3002<\/p>\n<p>\u90a3\u4e48\u63a5\u4e0b\u6765\u5c31\u6765\u5230\u4e86\u6211\u4eec\u7684\u91cd\u70b9\uff0c\u5982\u679c\u6211\u4eec\u60f3\u6784\u5efa\u4e00\u4e2a <code>TlsListener<\/code> \u5462\uff1f\u4f17\u6240\u5468\u77e5\uff0cTLS \u5fc5\u987b\u8981\u5728\u5176\u4ed6\u7684\u8fde\u63a5\uff08\u901a\u5e38\u662f TCP \uff09\u57fa\u7840\u4e0a\u8fdb\u884c\u6784\u5efa\uff0c\u90a3\u4e48\u6211\u4eec\u7684 TLS \u76d1\u542c\u5668\u5e94\u5f53\u6709\u4e00\u4e2a\u73b0\u6210\u7684 <code>Listener<\/code> \u4f5c\u4e3a\u6210\u5458\uff0c\u5e76\u5728\u6784\u9020\u65f6\u83b7\u53d6\u8fd9\u4e2a\u76d1\u542c\u5668\u7684\u6240\u6709\u6743\uff0c\u5373\uff1a<\/p>\n<pre><code class=\"language-rust\">struct TlsListener&lt;Ln&gt;\nwhere\n    Ln: Listener,\n{\n    ln: Ln,\n}\n\nimpl&lt;Ln&gt; TlsListener&lt;Ln&gt;\nwhere\n    Ln: Listener,\n{\n    fn new(base: Ln) -&gt; Self {\n        Self {\n            ln: base\n        }\n    }\n}\n<\/code><\/pre>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u5e94\u8be5\u4e3a <code>TlsListener<\/code> \u5b9e\u73b0 <code>Listener<\/code> trait \u4e86\uff0c\u5176\u57fa\u672c\u6846\u67b6\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><code class=\"language-rust\">impl&lt;Ln&gt; Listener for TlsListener&lt;Ln&gt;\nwhere\n    Ln: Listener,\n    Ln::Conn: Unpin + &#039;static,\n{\n    type Conn = ();\n    fn accept(&amp;self) {\n        \/\/ accept logic here...\n    }\n}\n<\/code><\/pre>\n<p>\u4e3a\u4e86\u8d34\u8fd1\u73b0\u5b9e\u573a\u666f\uff0c\u6211\u4eec\u4e3a <code>Ln::Conn<\/code> \u8bbe\u7f6e\u4e86\u4e24\u4e2a trait bound\uff0c\u5bf9\u4e8e\u771f\u5b9e\u7684\u5f02\u6b65\u670d\u52a1\u5668\u573a\u666f\uff0c\u8fd9\u4e2a bound \u4e2d\u8fd8\u5e94\u5f53\u5305\u62ec <code>Sync<\/code> \u548c <code>Send<\/code> \u3002\u4e0d\u8fc7\u8fd9\u4e9b\u90fd\u4e0d\u91cd\u8981\u4e86\uff0c\u56e0\u4e3a\u5230\u4e86\u8fd9\u4e00\u6b65\uff0c\u7f16\u8bd1\u9519\u8bef\u5c31\u51c6\u5907\u51fa\u73b0\u4e86\uff0c\u6211\u4eec\u4e0d\u5fc5\u518d\u7ea0\u7ed3 <code>accept<\/code> \u65b9\u6cd5\u7684\u903b\u8f91\u3002\u4e3a\u4e86\u5c55\u793a\u8fd9\u4e2a\u7f16\u8bd1\u9519\u8bef\uff0c\u53ea\u9700\u8981\u5f15\u5165\u4e24\u4e2a\u6d4b\u8bd5\u51fd\u6570\u5373\u53ef\uff1a<\/p>\n<pre><code class=\"language-rust\">fn test(_t: Pin&lt;Box&lt;dyn Unpin + &#039;static&gt;&gt;) {}\nfn test_ref(_t: &amp;Pin&lt;Box&lt;dyn Unpin + &#039;static&gt;&gt;) {}<\/code><\/pre>\n<p>\u6d4b\u8bd5\u7684\u5185\u5bb9\u90fd\u5f88\u6e05\u6670\uff0c\u552f\u4e00\u533a\u522b\u5c31\u662f\u4e00\u4e2a\u662f\u5f15\u7528\u800c\u53e6\u4e00\u4e2a\u4e0d\u662f\u3002\u73b0\u5728\u6211\u4eec\u5c55\u793a\u591a\u79cd\u53ef\u80fd\u7684\u5199\u6cd5\uff0c\u5e76\u7528\u6ce8\u91ca\u6807\u6ce8\u51fa\u7f16\u8bd1\u4e0d\u80fd\u901a\u8fc7\u7684\u6837\u4f8b\uff08\u73af\u5883\u662f rustc 1.78.0 2024-04-29\uff09\uff1a<\/p>\n<pre><code class=\"language-rust\">impl&lt;Ln&gt; Listener for TlsListener&lt;Ln&gt;\nwhere\n    Ln: Listener,\n    Ln::Conn: Unpin + &#039;static,\n{\n    type Conn = ();\n    fn accept(&amp;self) {\n        \/\/ ok\n        let conn = self.ln.accept();\n        test(Box::pin(conn));\n\n        \/\/ ok\n        let conn0 = self.ln.accept();\n        let boxed_conn0 = Box::new(conn0);\n        test(Pin::new(boxed_conn0));\n\n        \/\/ not ok\n        let conn1 = self.ln.accept();\n        test_ref(&amp;Box::pin(conn1));\n\n        \/\/ ok\n        let conn2: &lt;Ln as Listener&gt;::Conn = self.ln.accept();\n        let boxed_conn2 = Box::new(conn2);\n        test_ref(&amp;Pin::new(boxed_conn2));\n\n        \/\/ ok\n        let conn3 = self.ln.accept();\n        let boxed_conn3: Box&lt;dyn Unpin + &#039;static&gt; = Box::new(conn3);\n        test_ref(&amp;Pin::new(boxed_conn3));\n    }\n}<\/code><\/pre>\n<p><code>test<\/code> \u548c <code>test_ref<\/code> \u90fd\u662f\u4ec5\u7528\u4e8e\u6d4b\u8bd5\u7f16\u8bd1\u7c7b\u578b\u7684\u51fd\u6570\u3002\u5728\u4ee5\u4e0a\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5c06\u80fd\u901a\u8fc7\u7f16\u8bd1\u7684\u5199\u6cd5\u6807\u8bb0\u4e3a ok \uff0c\u53cd\u4e4b\u5219\u662f not ok\u3002\u5bf9\u4e8e\u65e0\u6cd5\u901a\u8fc7\u7f16\u8bd1\u7684\u90e8\u5206\uff0c\u62a5\u9519\u5982\u4e0b\uff1a<\/p>\n<pre><code>error[E0308]: mismatched types\n   --&gt; src\/main.rs:94:18\n    |\n94  |         test_ref(&amp;Box::pin(conn1));\n    |         -------- ^^^^^^^^^^^^^^^^ expected `&amp;Pin&lt;Box&lt;dyn Unpin&gt;&gt;`, found `&amp;Pin&lt;Box&lt;&lt;Ln as Listener&gt;::Conn&gt;&gt;`\n    |         |\n    |         arguments to this function are incorrect\n    |\n    = note: expected reference `&amp;Pin&lt;Box&lt;(dyn Unpin + &#039;static)&gt;&gt;`\n               found reference `&amp;Pin&lt;Box&lt;&lt;Ln as Listener&gt;::Conn&gt;&gt;`\nnote: function defined here\n   --&gt; src\/main.rs:108:4\n    |\n108 | fn test_ref(_: &amp;Pin&lt;Box&lt;dyn Unpin + &#039;static&gt;&gt;) {}\n    |    ^^^^^^^^ ----------------------------------\nhelp: consider constraining the associated type `&lt;Ln as Listener&gt;::Conn` to `(dyn Unpin + &#039;static)`\n    |\n77  |     Ln: Listener&lt;Conn = (dyn Unpin + &#039;static)&gt;,\n    |                 ++++++++++++++++++++++++++++++\n\nFor more information about this error, try `rustc --explain E0308`.<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u5f53\u6211\u4eec\u7684\u6d4b\u8bd5\u51fd\u6570\u63a5\u53d7\u7684\u5bf9\u8c61\u7c7b\u578b\u4e3a\u5f15\u7528\u65f6\uff0c\u7f16\u8bd1\u671f\u4fbf\u4e0d\u80fd\u5728 <code>Box::pin<\/code> \u4e2d\u81ea\u52a8\u5c06\u6211\u4eec\u7684\u53d8\u91cf\u8f6c\u6362\u4e3a <code>dyn Unpin + &#039;static<\/code> \u7684 trait object \u5f62\u5f0f\uff0c\u800c\u662f\u7559\u5728\u4e86\u5173\u8054\u7c7b\u578b <code>&lt;Ln as Listener&gt;::Conn<\/code> \u3002\u6b64\u65f6\u6211\u4eec\u53ea\u6709\u50cf <code>conn3<\/code> \u90a3\u6837\uff0c\u663e\u5f0f\u5730\u5c06 <code>Box<\/code> \u7684\u6cdb\u578b\u6307\u5b9a\u4e3a trait object\uff0c\u624d\u53ef\u4ee5\u901a\u8fc7\u7f16\u8bd1\u3002<\/p>\n<p>\u6b64\u65f6 rust \u7f16\u8bd1\u5668\u7ed9\u51fa\u7684\u5e2e\u52a9\u4e5f\u662f\u57fa\u672c\u4e0d\u53ef\u7528\u7684\uff1a<\/p>\n<blockquote>\n<p>&quot;consider constraining the associated type <code>&lt;Ln as Listener&gt;::Conn<\/code> to <code>(dyn Unpin + &#039;static)<\/code><\/p>\n<\/blockquote>\n<p>\u5982\u679c\u6211\u4eec\u771f\u7684\u5c06\u6cdb\u578b\u7684 trait bound \u5199\u6210 <code>Ln: Listener&lt;Conn = (dyn Unpin + &#039;static)&gt;,<\/code> \u90a3\u4e48\u4f1a\u51fa\u73b0\u52a8\u6001\u5206\u53d1\u7c7b\u578b\u5927\u5c0f\u4e0d\u53ef\u77e5\u7684\u95ee\u9898\uff0c\u8fd8\u9700\u8981\u518d\u53d6\u4e00\u4e2a <code>&amp;<\/code> \u6216\u662f <code>Box<\/code> \u5c06\u5176\u8f6c\u6362\u4e3a\u7279\u5f81\u5bf9\u8c61\u3002\u800c\u8fd9\u6837\u4fee\u6539\u7684\u8bdd\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u6539\u53d8 <code>Listener<\/code> trait \u672c\u8eab\uff0c\u8981\u6c42\u6240\u6709\u7684 <code>Listener<\/code> trait \u8fd4\u56de\u7684 <code>Conn<\/code> \u90fd\u662f\u52a8\u6001\u5206\u53d1\u7684\uff0c\u8fd9\u4e0b\u5c31\u8ba9\u7b80\u5355\u95ee\u9898\u590d\u6742\u5316\u4e86\u3002<\/p>\n<h2>2. \u6df1\u6316\u4e00\u4e0b<\/h2>\n<p>\u5728 rust users forum \u4e0a<a href=\"https:\/\/users.rust-lang.org\/t\/mismatched-types-error-on-associated-type-and-trait-objects\/111393\">\u53d1\u5e16<\/a>\u6c42\u52a9\u4e86\u4e00\u4e0b\uff0c\u56de\u5e16\u7684\u5927\u4f6c\u7684\u539f\u6587\u662f\uff1a<\/p>\n<blockquote>\n<p>Some bad order of inference is going on. You can't coerce a &amp;Box<T> or a &amp;Pin&lt;Box<T>&gt; to a &amp;Box<dyn Trait> or &amp;Pin&lt;Box<dyn trait>&gt; because of too much indirection. So the compiler decided you had created a &amp;Pin&lt;Box<AssocType>&gt; before it tried to coerce it.<\/p>\n<\/blockquote>\n<p>\u770b\u8d77\u6765 rust \u7f16\u8bd1\u5668\u5728\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u65f6\uff0c\u5982\u679c\u9700\u8981\u88ab\u8f6c\u6362\u7684\u7c7b\u578b\u88ab\u201c\u5305\u201d\u4e86\u592a\u591a\u5c42\uff0c\u5c31\u4e0d\u80fd\u63a8\u7406\u51fa\u6b63\u786e\u7684\u7c7b\u578b\u4e86\u3002\u8fd9\u4e2a\u5305\u88f9\u53ef\u4ee5\u662f <code>Pin<\/code> <code>Box<\/code> \u4e4b\u7c7b\u7684\u8303\u7574\uff0c\u4e5f\u80fd\u662f\u5f15\u7528\u3002<\/p>\n<p>\u540e\u9762\u6211\u4f1a\u5f00\u59cb\u7ffb\u8bd1\u8be5\u5927\u4f6c\u5199\u7684\u6709\u5173\u7279\u5f81\u5bf9\u8c61\u7684\u7cfb\u5217\u6587\u7ae0\u3002\u76f8\u4fe1\u5728\u5b8c\u5168\u6d88\u5316\u4e86\u90a3\u4e9b\u6587\u7ae0\u540e\uff0c\u80fd\u66f4\u52a0\u6df1\u5165\u7684\u7406\u89e3\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. \u573a\u666f \u5f53\u6211\u4eec\u9700\u8981\u7528\u7279\u5f81\u5bf9\u8c61\u7684\u65f6\u5019\uff0c\u5927\u90e8\u5206\u60c5\u51b5\u4e0b Rust \u7f16\u8bd1\u5668\u53ef\u4ee5\u81ea\u52a8\u8fdb\u884c\u7c7b\u578b\u63a8\u65ad\u3002\u4f46\u662f\u5728\u5c11\u6570\u573a\u666f\u4e0b\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-333","post","type-post","status-publish","format-standard","hentry","category-rust"],"_links":{"self":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts\/333","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/comments?post=333"}],"version-history":[{"count":0,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts\/333\/revisions"}],"wp:attachment":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/media?parent=333"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/categories?post=333"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/tags?post=333"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}